From: Francisco Laguna Date: 2007-06-15T06:48:52+09:00 Subject: Re: "case when" executes a symbol as method Thanks David and Devin for pointing that out! Looks like the syntax highlighting in my editor tricked my head into parsing that weirdly :D Am 14.06.2007 um 16:16 schrieb dblack@wobblini.net: > Hi -- > > On Thu, 14 Jun 2007, Francisco Laguna wrote: > >> Hi List! >> >> I just stumbled upon some interesting behaviour of case when and >> wanted >> to ask a few things about it. It looks like the "case when" construct >> executes a method when one leaves a out a newline (or semi-colon, I >> guess) after the when differentiation. Consider these to programs: >> >> === program 1 === >> >> def hello >> "Hello World!" >> end >> >> def good_day >> "Good Day, World!" >> end >> >> greeting = "Hello" >> >> puts case greeting >> when "Hello" >> :hello >> when "Good Day" >> :good_day >> end >> >> === program 2 === >> >> def hello >> "Hello World!" >> end >> >> def good_day >> "Good Day, World!" >> end >> >> greeting = "Hello" >> >> puts case greeting >> when "Hello" :hello >> when "Good Day" :good_day >> end >> >> ============== >> >> The first just prints out the symbols turned to strings ("hello" or >> "good_day", respectively), but the second one acrually executes the >> hello and good_day methods and the case block has the return value of >> the methods as its own value. Pretty cool, if you ask me. How >> come? Is >> that something I can rely on, or something that might disappear, >> because >> it's just some side-effect? > > I believe it's being parsed as: > > when "Hello": hello > > Note the : which can also separate the when part from the value. > > Don't be disappointed. It would be beyond bizarre if a symbol > suddenly decided to be a method call because of something like which > line it was written on. > > > David > > -- > * Books: > RAILS ROUTING (new! http://safari.awprofessional.com/9780321509246) > RUBY FOR RAILS (http://www.manning.com/black) > * Ruby/Rails training > & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) >