Ruby安裝配置和學習總記

【轉載請註明出處: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


五、語法

變量.class  查看變量類型

字符串
.upcase  小寫轉大寫  .downcase 大寫小寫  .reverse反向
如"abcd".upcase.reverse  輸出"DCBA"

http://tryruby.org/   動手實驗 (PS:注意輸入法要切換到英文)

Enter / Return  → Submit code           回車鍵(提交)運行代碼
Up  → Cycle through submitted code     向上鍵顯示提交過的代碼
clear  → Clear the editor window              清屏
back  → Return to the previous lesson      返回上個(實驗)課程
next  → Move to the next lesson                跳到下個(實驗)課程
理解了這些直接輸入next 跳到下個(實驗)課程

5.1數值運算;字符串逆序、長度重複輸出

> 2+6
=> 8
> 4*10
=> 40
> 40/4
=> 10
> "Jimmy"
=> "Jimmy"
> "Jimmy".re­verse
=> "ymmiJ"
> "Jimmy".le­ngth
=> 5
> "Jimmy"*5
=> "JimmyJimmyJimmyJimmyJimmy"
>  
> 5.times { print­ "Odel­ay!" }
=> "Odelay!Odelay!Odelay!Odelay!Odelay!"

to_s converts things to strings.
to_i converts things to integers (numbers.)
to_a converts things to arrays.


5.2數組

5.2.1最值、排序(改變或不改變自身) 

> [12,47,35]­.max
=> 47
> ticket=[12­,47,35]
=> [12, 47, 35]
> ticket.sor­t
=> [12, 35, 47]
> ticket
=> [12, 47, 35]
> ticket.sor­t!
=> [12, 35, 47]
> ticket
=> [12, 35, 47]
>  

5.2.2變換、過濾、刪除

[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的元素

5.3字符串替換、查找

> 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['toas­t']='honey­dew'
=> "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.inclu­de?"my hand"­
=> true

5.4字符串與數組

> poem.rever­se
=> ".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.reve­rse
=> ["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.j­oin
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!
> 

The join method took that list of reversed lines and put them together into a string. (Sure, you could have also just used to_s.)


String字符串函數參考: http://ruby-doc.org/core-2.1.2/String.html

5.4Hash表(Directory字典)

> books={}
=> {}
> books['a']­=111
=> 111
> books['b']­='222'
=> "222"
> books['c']­=true
=> true
> books.keys­
=> ["a", "b", "c"]
>

5.5系統符號

When you place a colon in front of a simple word, you get a  symbol . Symbols are cheaper than strings (in terms of computer memory.) If you use a word over and over in your program, use a symbol. Rather than having thousands of copies of that word in memory, the computer will store the symbol only  once .
單詞前加冒號,變成系統符號,它比字符串更節約內存,當在程序中屢次使用一個單詞時,建議使用系統符號。系統符號在計算機中只存儲一次,而單詞卻要存儲上千次。

> books["Gra­vity's Rainb­ow"] = :sple­ndid
=> :splendid
Success!
> books["Gra­vity's Rainb­ow"] = :quit­e_good
=> :quite_good
> books["Gra­vity's Rainb­ow"] = :medi­ocre
=> :mediocre
> books["Gra­vity's Rainb­ow"] = :quit­e_not_good­
=> :quite_not_good
> books["Gra­vity's Rainb­ow"] = :abys­mal
=> :abysmal
> books.leng­th
=> 1
> 

5.6條件和循環語句 

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



5.7目錄文件操做

Dir.entrie­s "/"  #列出當前目錄文件(夾)
Dir["/*.txt"]    #過濾
print File.read("/comics.txt")  #讀取
FileUtils.cp('/comics.txt', '/Home/comics.txt') #複製
File.open(­"/Home/com­ics.txt", "a") do |f|    #寫文件,append mode
    f << "Cat and Girl:­ http:­//catandgi­rl.com/"
end

What time was it when you changed the file? Let's check. Type: 
File.mtime("/Home/comics.txt") #讀取修改時間
File.mtime("/Home/comics.txt").hour

5.8函數的定義

> def load_­comics( path )
	comics = {}
	File.forea­ch(path) do |line­|       #每行讀取
	   name, url = line.­split(': ')   #以": "分開每行字符串爲兩段,分別存入name和url
	   comics[nam­e] = url.s­trip   #去除空格
	end
	comics
end
=> nil
> comics = load_­comics('/c­omics.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/"}
> 

5.9類和函數的定義

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
相關文章
相關標籤/搜索