先把各種熱門的東西都走馬觀花地看一遍,呵呵。
看的是Everyday Scripting with Ruby,風格和In Action系列差不多,大量的實例。
現在學Ruby的主要目的也是everyday scripting,方便數據處理、生成,文件批處理等,RoR之類的暫時不考慮。
1. String.inspect 方法
文檔中的說法是
str.inspect => string
Returns a printable version of _str_, with special characters
escaped.
str = "hello"
str[3] = 8
str.inspect #=> "hel\010o"
具體情況試試 myString.inspect.inspect....就能了解一點了
2. Arrays.each 和 Arrays.collect
for_each方法
irb(main):007:0> [1, 2, 3].each do | element |
irb(main):008:1* puts element
irb(main):009:1> end
1
2
3
=> [1, 2, 3]
后者與前者的不同之處在于,在處理數據的同時,每次處理的返回結果都會保存到一個新的數組中返回。
irb(main):036:0> newarray = ["aBC", "B"].collect do |e|
irb(main):037:1* e.downcase
irb(main):038:1> end
=> ["abc", "b"]
3. Messages and Methods
It can be hard to remember the difference between messages and methods. A message is a request sent from some sender object. When the receiver object receives the message, it looks to see whether it has a method with the same name. If so, the Ruby code within the method is run, and the results are returned to the sender. The message is the request; the method fulfills it.
呃,還是沒有感性認識。
4. Delimiting Blocks
塊的兩種表示方式:
array.each do | element |
puts element
end
array.each { | element |
puts element
}
通常使用第一種,但可以用一行寫成的情況也可以使用第二種:
array.each { | element | puts element }
看的是Everyday Scripting with Ruby,風格和In Action系列差不多,大量的實例。
現在學Ruby的主要目的也是everyday scripting,方便數據處理、生成,文件批處理等,RoR之類的暫時不考慮。
1. String.inspect 方法
文檔中的說法是
str.inspect => string
Returns a printable version of _str_, with special characters
escaped.
str = "hello"
str[3] = 8
str.inspect #=> "hel\010o"
具體情況試試 myString.inspect.inspect....就能了解一點了
2. Arrays.each 和 Arrays.collect
for_each方法
irb(main):007:0> [1, 2, 3].each do | element |
irb(main):008:1* puts element
irb(main):009:1> end
1
2
3
=> [1, 2, 3]
后者與前者的不同之處在于,在處理數據的同時,每次處理的返回結果都會保存到一個新的數組中返回。
irb(main):036:0> newarray = ["aBC", "B"].collect do |e|
irb(main):037:1* e.downcase
irb(main):038:1> end
=> ["abc", "b"]
3. Messages and Methods
It can be hard to remember the difference between messages and methods. A message is a request sent from some sender object. When the receiver object receives the message, it looks to see whether it has a method with the same name. If so, the Ruby code within the method is run, and the results are returned to the sender. The message is the request; the method fulfills it.
呃,還是沒有感性認識。
4. Delimiting Blocks
塊的兩種表示方式:
array.each do | element |
puts element
end
array.each { | element |
puts element
}
通常使用第一種,但可以用一行寫成的情況也可以使用第二種:
array.each { | element | puts element }