From: Robert Klemme Date: 2007-03-16T00:45:08+09:00 Subject: Re: What does this do? : On 15.03.2007 15:54, Stefano Crocco wrote: > Alle gioved� 15 marzo 2007, jko170 ha scritto: >> Looking at this piece of code, what does the colon do? >> >> def partner_to(user) >> raise ArgumentError unless user >> user.id == recipient_id ? sender : recipient >> end >> >> I got the code from this rails post: >> http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7fdccd >> dde9b97ffc/6112294580b9e0ce?lnk=gst&q=internal+mail&rnum=1#6112294580b9e0ce >> >> I'm just asking because I've never seen the colon used before. > > This is a shortcut for > if user.id == recipient_id ? then sender > else recipient > end You got a question mark too much in there. :-) > What comes before the ? is the condition (not to confuse with the ? which ends > the name of predicate methods: array.empty? do_something : do_something_else > won't work, array.empty? ? do_something : do_something_else will), after > the ? and before the : there's the action to take if the condition is true > and after the : what to do if it's false. Yes. robert