# Help with ? yield :

**URL:** <https://www.ruby-forum.com/t/help-with-yield/122001>\
**Category:** Ruby\
**Created:** [December 3, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/help-with-yield/122001 "2007-12-03T19:56:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 3, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/help-with-yield/122001/1 "2007-12-03T19:56:08Z")

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

```
   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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 3, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/help-with-yield/122001/2 "2007-12-03T20:07:18Z")

</div>

? is the equivalent to ‘if’ kinda… For example

a = 10  
if a == 10 then else end

is the same is

a == 10 ? :

Hope that helped.

Regards,  
Lee

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 3, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/help-with-yield/122001/3 "2007-12-03T20:12:59Z")

</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

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 3, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/help-with-yield/122001/4 "2007-12-03T20:24:36Z")

</div>

thanks to all !!

now is clear!

Raimon

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 3, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/help-with-yield/122001/5 "2007-12-03T20:14:09Z")

</div>

On Dec 3, 1:56 pm, Raimon Fs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> session[:intended_action] = action_name
> 
> ```
> 
> thanks!
> 
> ## raimon
> 
> Posted viahttp://www.ruby-forum.com/.

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