From: Stefano Crocco Date: 2007-03-15T23:54:46+09:00 Subject: Re: What does this do? : 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 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. Stefano