概括、ruby中的基礎資料型別 (Elementary Data Type)都是類類型,只不過為了書寫和閱讀的方便,為基礎資料型別 (Elementary Data Type)提供了較為簡潔的書寫方式而已。一、數實值型別整數支援二進位,八進位,十進位,十六進位,根據整數的大小動態決定整數是Fixnum類型還是Bignum類型。浮點數支援科學計數法,小數點後至少有一個數字。數實值型別繼承圖如下:複製代碼 代碼如下:Numeric |--Integer &
1、在proc和lambda中,return關鍵字有不同含義:在proc中,return僅僅表示從這個lambda中返回.在lambda中,return不是從proc中返回,而是從定義proc的範圍中返回.複製代碼 代碼如下:def one_method p = Proc.new{return 10} #調用這個塊的時候,從p的範圍直接返回10,因此下面的return將不會執行 result =
動態規劃解決矩陣連乘問題,隨機產生矩陣序列,輸出形如((A1(A2A3))(A4A5))的結果。代碼:#encoding: utf-8=beginauthor: xu jin, 4100213date: Oct 28, 2012MatrixChainto find an optimum order by using MatrixChain algorithmexample output:The given array is:[30, 35, 15, 5, 10, 20, 25]The
最長公用子序列,LCS,動態規劃實現。#encoding: utf-8#author: xu jin, 4100213#date: Nov 01, 2012#Longest-Commom-Subsequence#to find a longest commom subsequence of two given character arrays by using LCS algorithm#example output:#The random character arrays are: ["b",
利用動態規划算法,實現最短編輯距離的計算。複製代碼 代碼如下:#encoding: utf-8#author: xu jin#date: Nov 12, 2012#EditDistance#to find the minimum cost by using EditDistance algorithm#example output:# "Please input a string: "# exponential# "Please input the
演算法導論上的偽碼改寫而成,加上導論的課後練習第一題的解的建構函式。複製代碼 代碼如下:#encoding: utf-8=beginauthor: xu jindate: Nov 11, 2012Optimal Binary Search Treeto find by using EditDistance algorithmrefer to <<introduction to algorithms>>example output:"k2 is the root of
#建立一個222.rb檔案並且輸入字元file = File.open("222.rb","w+")file.puts "123\nwadwa\n12124124\ndwdw"file.close#輸出222.rb的內容File.open("222.rb","r+") do |file|while line = file.getsputs lineendend#直接用IO操作檔案IO.foreach("222.rb") do |line|puts line if line =~/abc/
一、建立檔案複製代碼 代碼如下: f=File.new(File.join("C:","Test.txt"), "w+") f.puts("I am Jack") f.puts("Hello World")檔案模式"r" :Read-only. Starts at beginning of file (default mode)."r+" :Read-write. Starts at