Jun 29, 2010 @ 12:12 pm

A quick and easy tip today. It won’t impress the veteran ruby developer, but it will impress every ruby newcomers, guaranteed!

Say I have a horse racing ruby application that allow people to know information about every competing horses. I have an input box that people can use to enter the name of a horse. Once they click OK, information about the specified horse is displayed to the user.

Instead of going the traditonal route, how about adding a “horse” method to the String class? It could not be easier :

  1. HORSES = [
  2.   {
  3.     :name => "Jolly Jumper",
  4.     :also_known_as => ["Smart Arse","Lucky Luke friend"],
  5.     :speed => 3, :intelligence => 30,
  6.     :favorite_quote => "I am a horse, I don’t have quotes"
  7.   },
  8.  
  9.   {
  10.     :name => "Incredibly Bad",
  11.     :also_known_as => ["Shame","The Pathetic"],
  12.     :speed => 2, :intelligence => 4,
  13.     :favorite_quote => "If you still put your money on me, you only have yourself to blame"
  14.   },
  15.  
  16.   {
  17.     :name => "Fast And Furious",
  18.     :also_known_as => ["The Strong Rebel","The Furious One"],
  19.     :speed => 50, :intelligence => 15,
  20.     :favorite_quote => "I am fast"
  21.   },
  22.  
  23.  {
  24.     :name => "Slow and Jerky",
  25.     :also_known_as => ["Tender","Terrible Choice"],
  26.     :speed => 1, :intelligence => 10,
  27.     :favorite_quote => "Pick me once, regret it for a lifetime."
  28.   }
  29. ]
  30.  
  31. class String
  32.   def horse
  33.     res = HORSES.select { |h| h[:name] == self || h[:also_known_as].include?(self)}
  34.     res.first if res.length>0
  35.   end
  36. end
  37.  
  38. #Then you can do stuff like this :
  39. horse = "Tender".horse
  40. if horse
  41.         puts "#{horse[:name]}, also known as #{horse[:also_known_as].join(", ")}, has a speed of #{horse[:speed]} and an intelligence of #{horse[:intelligence]}. In its free time, this horse like to become all philosophic, look at the sky and say : ‘#{horse[:favorite_quote]}’"
  42. else
  43.         puts "This horse doesn’t exists in our records"
  44. end

Talk about some great syntactic ruby sugar!

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Posted under : short & sweet
2 Comments

Remember people to be nice in your comments. I have no problem if you formulate a constructive remark but offensive comments will be removed as soon as they get here. You stay polite or you go elsewhere, it’s that simple. Now I will put an emoticon to convince you all that I am not angry: :)

Comment by : FrankNo Gravatar
— June 29, 2010 @ 4:30 pm

class String
def horse
HORSES.detect { |h| h[:name] == self || h[:also_known_as].include?(self) }
end
end

Comment by : JustinNo Gravatar
— July 1, 2010 @ 1:19 pm




Leave a comment
Name (required)
Email (will not be publish) (required)
Website