# Help with ? yield :

**URL:** <https://rubytalk.org/t/help-with-yield/42706>\
**Category:** ruby-talk\
**Created:** [3 December 2007 18:56 UTC](https://rubytalk.org/t/help-with-yield/42706 "2007-12-03T18:56:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [3 December 2007 18:56 UTC](https://rubytalk.org/t/help-with-yield/42706/1 "2007-12-03T18:56:05Z")

</div>

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: ?

> **···**
>
> --------------------------------------
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:auth] ? yield : (  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:intended\_action] = action\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:intended\_controller] = controller\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'You need to be logged in to access this panel'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session\_update\_time  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all\_ok = 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:template =\> 'login/index'))
> 
> I understand what it does, but not what means:
> 
> ? yield : ( ... some code ...)
> 
> thanks!
> 
> raimon  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Lee\_Jarvis2](https://avatars.discourse-cdn.com/v4/letter/l/f6c823/32.png) [@Lee\_Jarvis2](https://rubytalk.org/u/Lee_Jarvis2)\
**Post date:** [3 December 2007 19:07 UTC](https://rubytalk.org/t/help-with-yield/42706/2 "2007-12-03T19:07:18Z")

</div>

? 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-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [3 December 2007 19:08 UTC](https://rubytalk.org/t/help-with-yield/42706/3 "2007-12-03T19:08:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Noah\_Easterly](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@Noah\_Easterly](https://rubytalk.org/u/Noah_Easterly)\
**Post date:** [3 December 2007 19:10 UTC](https://rubytalk.org/t/help-with-yield/42706/4 "2007-12-03T19:10:04Z")

</div>

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).

> **···**
>
> 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: ?  
> > --------------------------------------
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:auth] ? yield : (  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:intended\_action] = action\_name  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session[:intended\_controller] = controller\_name  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'You need to be logged in to access this panel'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session\_update\_time  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all\_ok = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:template =\> 'login/index'))
> > 
> > I understand what it does, but not what means:
> > 
> > ? yield : ( ... some code ...)
> > 
> > thanks!
> > 
> > raimon  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)\
**Post date:** [3 December 2007 19:24 UTC](https://rubytalk.org/t/help-with-yield/42706/5 "2007-12-03T19:24:33Z")

</div>

thanks to all !!

now is clear!

Raimon

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
