`

娃娃鸭学Ruby-36、枚举器

阅读更多
枚举器

枚举器是类Enumeralbe::Enumerator的实例
可以用new直接实例化,
但通常情况下使用Object类的to_enum或同义词enum_for方法

s="hello"
s.enum_for(:each_char).map{|c| c.succ} #=>["i","f","m","m","p"]

enumerator=3.times
enumerator.each{|x| print x} #"012"

10.downto(1).select{|x|x%2==0} # =>[10,8,6,5,2]

2011-4-19 15:06 danny

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics