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.

About Aimred

Aimred is a specialist Ruby and Ruby on Rails development house and consultancy based in Cape Town, South Africa.

We provide Ruby and Ruby on Rails development, consulting and training services to businesses and organisations of all sizes. If you want to find out how we can help you, contact us at info@aimred.com.

Recent Posts

Yearly Archives