Can procs be used with case statements in Ruby 2.0?
我记得Ruby2.0中的
我尝试查看Ruby2.0.0新闻以及如何用Ruby编写switch语句。我还访问了http://ruby-doc.org,但是它的关键字链接是Ruby1.9,而不是Ruby2.0。
在case语句中是否允许procs?
对。
1 2 3 4 5 6 7 8 9 10 11 12 | 2.0.0p0 :001> lamb = ->(x){ x%2==1 } #=> #<Proc:0x007fdd6a97dd90@(irb):1 (lambda)> 2.0.0p0 :002> case 3; when lamb then p(:yay); end :yay #=> :yay 2.0.0p0 :003> lamb === 3 #=> true 2.0.0p0 :007> lamb === 2 #=> false |
然而,这与1.9.1没有什么不同,因为
Invokes the block with
obj as the proc's parameter like#call . It is to allow a proc object to be a target ofwhen clause in acase statement.
对于Ruby初学者来说,Ruby的
1 2 3 4 | case"cats" when /^cat/ then puts("line starts with cat!") when /^dog/ then puts("line starts with dog!") end |
…运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Moddable = Struct.new(:n) do def ===(numeric) numeric % n == 0 end end mod4 = Moddable.new(4) mod3 = Moddable.new(3) 12.times do |i| case i when mod4 puts"#{i} is a multiple of 4!" when mod3 puts"#{i} is a multiple of 3!" end end #=> 0 is a multiple of 4! #=> 3 is a multiple of 3! #=> 4 is a multiple of 4! #=> 6 is a multiple of 3! #=> 8 is a multiple of 4! #=> 9 is a multiple of 3! |