[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help with ? yield :

Raimon Fs

12/3/2007 6:56:00 PM

Hello,

I'm new to Ruby and Rails, and I'm learning at the same time I'm reading
books and following some threads on the web, and of course,
experimenting ...

What's the meaning of this: ?
--------------------------------------

session[:auth] ? yield : (
session[:intended_action] = action_name
session[:intended_controller] = controller_name
flash[:notice] = 'You need to be logged in to access this panel'
session_update_time
all_ok = 0
render(:template => 'login/index'))

I understand what it does, but not what means:

? yield : ( ... some code ...)


thanks!

raimon
--
Posted via http://www.ruby-....

4 Answers

noah.easterly@gmail.com

12/3/2007 7:05:00 PM

0

On Dec 3, 1:56 pm, Raimon Fs <co...@montx.com> wrote:
> Hello,
>
> I'm new to Ruby and Rails, and I'm learning at the same time I'm reading
> books and following some threads on the web, and of course,
> experimenting ...
>
> What's the meaning of this: ?
> --------------------------------------
>
> session[:auth] ? yield : (
> session[:intended_action] = action_name
> session[:intended_controller] = controller_name
> flash[:notice] = 'You need to be logged in to access this panel'
> session_update_time
> all_ok = 0
> render(:template => 'login/index'))
>
> I understand what it does, but not what means:
>
> ? yield : ( ... some code ...)
>
> thanks!
>
> raimon
> --
> Posted viahttp://www.ruby-....

so ? : is normally called the trinary operator, and is common to a
bunch of languages (C, Ruby, Javascript, etc ad nauseum).

In ruby,

a?b:c

is shorthand for

if a
b
else
c
end

( not so much in C, since if statements don't have values in C )

So session[:auth] ? yield : ( ... ) either yields to a block passed by
the calling function, or executes the code defined in (...), depending
on whether session[:auth] is truish (not false or nil).

Lee Jarvis

12/3/2007 7:07:00 PM

0

? is the equivalent to 'if' kinda.. For example

a = 10
if a == 10 then <something> else <something else> end

is the same is

a == 10 ? <something> : <something else>


Hope that helped.

Regards,
Lee
--
Posted via http://www.ruby-....

Sebastian Hungerecker

12/3/2007 7:08:00 PM

0

Raimon Fs wrote:
> I understand what it does, but not what means:
>
> ? yield : ( ... some code ...)

foo ? bar : baz
is equivalent to
if foo then bar else baz end

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Raimon Fs

12/3/2007 7:25:00 PM

0

thanks to all !!

now is clear!

Raimon
--
Posted via http://www.ruby-....