This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: Frosted MiniColons
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
It must have been around revision 1.260 of parse.y that colons came into expression play. And, progressively, colons are getting more airtime in Ruby 1.9. Yet another thing I forgot to mention in What’s Shiny and New in Ruby 1.8.0?
Rather handy for laying out a short if..elsif series:
unit_of_time =
if num < 60: "second"
elsif num < 60 * 60: "minute"
elsif num < 60 * 60 * 24: "hour"
else "day"
end
Also, in case..when:
unit_of_time =
case num
when 0..60: "second"
when 0..60 * 60: "minute"
when 0..60 * 60 * 24: "hour"
else "day"
end
Did you know you can also use keyword then in place of all the colons above?
And if you want to your colon use to come to a head, try this in Ruby 1.9:
me = {name: "_why", url: "http://redhanded.hobix.com",
photo: "http://whytheluckystiff.net/i.home/boy.gif"}