A Workaround To A Near Miss
In my previous article I discussed how I thought Symbol#to_proc and Proc#=== would work nicely together but ran into syntax and semantic issues. Here’s how to get round that using a function that returns dynamically created Proc objects.
1 def it_is( property ) 2 Proc.new{ |object| object.send( property )} 3 end 4 5 case number 6 when it_is( :even? ) 7 puts "Even!" 8 when it_is( :odd? ) 9 puts "Odd!" 10 when it_is( :zero? ) 11 puts "Zero!" 12 end
Farrel Lifson is a lead developer at Aimred.