Aimred Developer Blog November 2008 Archive

A Quick Cautionary Tale

Just a quick word of warning, when you want to write

array = [ 1, 2, 3, 4 ]

don’t write

array = { 1, 2, 3, 4 }

the latter in fact being equivalent to writing

array = { 1 => 2, 3 => 4 }

and will keep you up till 3am trying to understand why your code is broken.

November Cape Town Ruby Brigade

The November meeting of the Cape Town Ruby Brigade is this coming Wednesday.

Talks:

  • ‘Irbie – A Ruby IRC Bot’ by Jonathan Groll
  • ’What’s New In Ruby 2.0’ by Farrel Lifson

Venue: Bandwidth Barn Meeting Room, 125 Buitengracht, Cape Town
Date: Wednesday, 12th November 2008
Time: 19:00

A Workaround To A Near Miss Part II

Following on from my previous article here’s a slightly neater way to combine Proc#=== and Symbol#to_proc. No special functions required.

1 case number
2 when lambda( &:even? )
3   puts "Even!"
4 when lambda( &:odd? )
5   puts "Odd!"
6 when lambda( &:zero? )
7   puts "Zero!"
8 end

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

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.