【轉載請註明出處:http://blog.csdn.net/leytton/article/details/37411721】
html
win7 ruby 1.9.3p545jquery
蘇勇老師Ruby開發語言視頻教程 http://edu.51cto.com/course/course_id-1414.html
數組
在瀏覽器上試用 Ruby http://tryruby.org/levels/1/challenges/0 (PS:注意輸入法要切換到英文)瀏覽器
20分鐘體驗 Ruby https://www.ruby-lang.org/zh_cn/documentation/quickstart/
ruby
文檔 - Ruby 官方網站 https://www.ruby-lang.org/zh_cn/documentation/
app
rubymonk教程 http://rubymonk.com/
less
Ruby系列博文 http://blog.csdn.net/aotian16/article/category/1073898
ide
下載 Ruby - Ruby 官方網站 https://www.ruby-lang.org/zh_cn/downloads/ 函數
我用win7開發的,因此下載了rubyinstaller http://rubyinstaller.org/
oop
根據提示"If you don’t know what version to install and you’re getting started with Ruby, we recommend you use Ruby 1.9.3 installers. These provide a stable language and a extensive list of packages (gems) that are compatible and updated."
我選擇下載Ruby 1.9.3-p545
安裝rubyinstaller時注意勾選「Add Ruby executable to your PATH」 把Ruby路徑加入環境變量中,這樣才能在命令行直接輸入ruby便可使用
安裝好後命令行輸入ruby -v便可查看ruby版本,並肯定ruby能正常使用
1>把代碼寫好保存文件爲*.by 命令行執行 ruby *.by便可運行
2>命令行敲入ruby便可進入代碼編寫模式,寫好代碼,按Ctrl+D後回車便可執行
具體參考 http://blog.sina.com.cn/s/blog_4881040d01012kj7.html
clear
→ Clear the editor window 清屏
back
→ Return to the previous lesson 返回上個(實驗)課程
next
→ Move to the next lesson 跳到下個(實驗)課程
> 2+6 => 8 > 4*10 => 40 > 40/4 => 10 > "Jimmy" => "Jimmy" > "Jimmy".reverse => "ymmiJ" > "Jimmy".length => 5 > "Jimmy"*5 => "JimmyJimmyJimmyJimmyJimmy" >
> 5.times { print "Odelay!" } => "Odelay!Odelay!Odelay!Odelay!Odelay!"
> [12,47,35].max => 47 > ticket=[12,47,35] => [12, 47, 35] > ticket.sort => [12, 35, 47] > ticket => [12, 47, 35] > ticket.sort! => [12, 35, 47] > ticket => [12, 35, 47] >
[1, 2, 3, 4, 5].map { |i| i + 1 } #變換 輸出[2, 3, 4, 5, 6] [1,2,3,4,5,6].select {|number| number % 2 == 0} #過濾 輸出[2,4,6] names = ['rock', 'paper', 'scissors', 'lizard', 'spock'] names.select {|word| word.length > 5} #過濾 輸出長度大於5的元素 [1,2,3,4,5,6,7].delete_if{|i| i < 4 } #刪除小於4的元素
> print poem => "My toast has flown from my hand And my toast has gone to the moon. But when I saw it on television, Planting our flag on Halley's comet, More still did I want to eat it." Success! > poem['toast']='honeydew' => "honeydew" Success! > print poem => "My honeydew has flown from my hand And my toast has gone to the moon. But when I saw it on television, Planting our flag on Halley's comet, More still did I want to eat it." >
> poem.include?"my hand" => true
> poem.reverse => ".ti tae ot tnaw I did llits eroM ,temoc s'yellaH no galf ruo gnitnalP ,noisivelet no ti was I nehw tuB .noom eht ot enog sah tsaot ym dnA dnah ym morf nwolf sah wedyenoh yM" Success! > poem.lines.to_a.reverse => ["More still did I want to eat it. ", "Planting our flag on Halley's comet, ", "But when I saw it on television, ", "And my toast has gone to the moon. ", "My honeydew has flown from my hand "] Success! > print poem.lines.to_a.reverse.join More still did I want to eat it. Planting our flag on Halley's comet, But when I saw it on television, And my toast has gone to the moon. My honeydew has flown from my hand Success! >
join
method took that list of reversed lines and put them together into a string. (Sure, you could have also just used to_s
.)> books={} => {} > books['a']=111 => 111 > books['b']='222' => "222" > books['c']=true => true > books.keys => ["a", "b", "c"] >
> books["Gravity's Rainbow"] = :splendid => :splendid Success! > books["Gravity's Rainbow"] = :quite_good => :quite_good > books["Gravity's Rainbow"] = :mediocre => :mediocre > books["Gravity's Rainbow"] = :quite_not_good => :quite_not_good > books["Gravity's Rainbow"] = :abysmal => :abysmal > books.length => 1 >
def check_sign(number) if number > 0 "#{number} is positive" else "#{number} is negative" end end
def check_sign(number) number > 0 ? "#{number} is positive" : "#{number} is negative" end
Dir.entries "/" #列出當前目錄文件(夾) Dir["/*.txt"] #過濾 print File.read("/comics.txt") #讀取 FileUtils.cp('/comics.txt', '/Home/comics.txt') #複製 File.open("/Home/comics.txt", "a") do |f| #寫文件,append mode f << "Cat and Girl: http://catandgirl.com/" end
File.mtime("/Home/comics.txt") #讀取修改時間
File.mtime("/Home/comics.txt").hour
> def load_comics( path ) comics = {} File.foreach(path) do |line| #每行讀取 name, url = line.split(': ') #以": "分開每行字符串爲兩段,分別存入name和url comics[name] = url.strip #去除空格 end comics end => nil > comics = load_comics('/comics.txt') => {"Achewood"=>"http://achewood.com/", "Dinosaur Comics"=>"http://qwantz.com/", "Perry Bible Fellowship"=>"http://cheston.com/pbf/archive.html", "Get Your War On"=>"http://mnftiu.cc/"} >
class Rectangle def initialize(length, breadth) @length = length @breadth = breadth end def perimeter 2 * (@length + @breadth) end #write the 'area' method here end
<span style="color: rgb(68, 68, 68); font-family: monospace, sans-serif; line-height: 24px; background-color: rgb(248, 248, 255); ">Rectangle.new.perimeter</span>類變量 http://blog.csdn.net/aotian16/article/details/7291036