2007年1月7日星期日

Ruby Cookbook

终于狠了心买了《Ruby Cookbook》,电子版本看着太费眼神,纸质的感觉始终留着书香:)决定好好看看Ruby,因为写RoR的时候,感觉Ruby基础太弱,以致于很多概念,包括Plugin的使用都不是很顺手,以后每天都抽出时间看几个Recipe,嘿嘿,Ok,let's
look at it.


Recipse 1.1 Building a String from Parts


hash = {'key1'=>'val1','key2'=>'val2'}
string = ""



# don't code like following,
# because it makes new String object,wastes time and memory
hash.each {|k,v| string << "#{k} is #{v}\n"}



# more efficient way like this:
hash.each {|k,v| string << k << " is " << v << "\n" }



data = ['1','2','3']
data.join('|')
# => 123
# another way to simulate the behavio of Array#join across an iteration
s=""
data.each_with_index {|x,i| s << x; s << "|" if i < data.length-1 }
puts s
# => 123


1 条评论:

琳琳的小狗 说...

呵呵,我也打算学ruby了,不过最近没有时间,要钻研javascript,等下个月跟你学啊!