Jason, The standard ternary (like Kevin shows above) is Ruby's ternary... what Kev means when he says it 'comes from C' is that the construct exists in Ruby because it also exists in other C-like languages. C does appear to be the language where the ?: syntax originated.
The if;else;end notation is really discouraged in practice, since it is more obtuse than a normal line end if block. The ternary has 2 things going for it: its concise and its (marginally) faster than the if block. If statements have two things going for them: its more readable and its more flexible. Turning the if statement into a one liner doesn't seem to have either set of advantages. One last note about the ternary is that some C best practice guides claim that the only time one should use the ternary is for assignment. Since we have if block assignment in Ruby [1] and its common to return the ternary in otherwise immaculate Ruby code, I don't personally think that rule holds true Rob [1] ie: hello = if true "hello" else "goodbye" end hello => "hello" On Tue, Mar 3, 2009 at 15:44, Jason King <[email protected]> wrote: > Ruby's ternary: > render (if a.exists?; a; else; b; end) > - Show quoted text - > > On 04/03/2009, at 10:32 AM, Jordan Fowler wrote: > > Stick with whatever you prefer. If you're akin to brevity, go with tertiary. > If you're more concerned with making your code as readable as possible, go > with the formal if else statement. > -Jordna > On Mar 3, 2009, at 3:29 PM, Matt Aimonetti wrote: > > That's actually a good argument for not using the ternary operator :p > I think i'd go with Kevin's suggestion... stick to what you have, it's > simple and obvious. > > - Matt > > On Tue, Mar 3, 2009 at 3:25 PM, David A McClain <[email protected]> > wrote: >> >> Oh I see, thanks Kevin et al. >> >> You learn something new everyday. Somedays you're lucky enough to >> learn something useful! >> >> >> >> On Mar 3, 2009, at 15:23, Kevin Clark <[email protected]> wrote: >> >> > >> > ? a : b >> > >> > Is from C. It means: (condition) ? (if true) : (if false) >> > >> > (1 + 1 == 2) ? "foo" : "bar" # => "foo" >> > >> > On Tue, Mar 3, 2009 at 3:16 PM, David A McClain <[email protected] >> > > wrote: >> >> I can understand what that code is doing because I know the >> >> context, but >> >> otherwise I'd be lost. >> >> What does the "? a : b" bit actually do? I've never seen the colon >> >> operator. >> >> >> >> >> >> >> >> On Mar 3, 2009, at 14:02, Jordan Fowler <[email protected]> wrote: >> >> >> >> I would probably just use tertiary as the argument to render: >> >> render a.exists? ? a : b >> >> -Jordan >> >> On Mar 3, 2009, at 12:59 PM, Guyren G Howe wrote: >> >> >> >> What's the most elegant way to do: >> >> >> >> if a exists >> >> render a >> >> else >> >> render b >> >> end >> >> >> >> ? >> >> >> >> I was going to write a helper that just relies on the exception, >> >> but I >> >> wondered if there was already a concise way to do this. >> >> >> >> >> >> >> >> >> >> >> >> >> >> --------------------------------------------------------------- >> >> Jordan A. Fowler >> >> 2928 Fir St. >> >> San Diego, CA 92102 >> >> E-mail: [email protected] >> >> Website: http://www.jordanfowler.com >> >> Phone: (619) 339-6752 >> >> >> >> >> >> >> >>> >> >> >> > >> > >> > >> > -- >> > Kevin Clark >> > http://glu.ttono.us >> > >> > > >> >> > > > > > > > > --------------------------------------------------------------- > Jordan A. Fowler > 2928 Fir St. > San Diego, CA 92102 > E-mail: [email protected] > Website: http://www.jordanfowler.com > Phone: (619) 339-6752 > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ SD Ruby mailing list [email protected] http://groups.google.com/group/sdruby -~----------~----~----~----~------~----~------~--~---
