# What does this do? :

**URL:** <https://www.ruby-forum.com/t/what-does-this-do/88915>\
**Category:** Ruby\
**Created:** [March 15, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915 "2007-03-15T15:50:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/1 "2007-03-15T15:50:33Z")

</div>

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/7fdccddde9b97ffc/6112294580b9e0ce?lnk=gst&q=internal+mail&rnum=1#6112294580b9e0ce](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7fdccddde9b97ffc/6112294580b9e0ce?lnk=gst&q=internal+mail&rnum=1#6112294580b9e0ce)

I’m just asking because I’ve never seen the colon used before.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/2 "2007-03-15T15:55:34Z")

</div>

Alle giovedì 15 marzo 2007, jko170 ha scritto:

> 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

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/3 "2007-03-15T15:57:34Z")

</div>

You have to see the statement in its entirety. Its centered around the  
ternary operator ?:

What the code is doing is:  
if (user.id == recipient\_id) {  
sender  
} else {  
recipient  
}

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/4 "2007-03-15T16:11:40Z")

</div>

Looks like a ternary operator (you know binary and unary operators,  
this is the only ternary I’ve ever seen)  
exists in C and C like languages such as PHP

conditional\_statement ? true\_result : false\_result

if ()  
a  
else  
b

written as  
if\_condition ? result\_a : result\_b

one of the less lovely things to read in C like languages, but after  
you get used to it, it is actally a space saver.  
It is relly just an conditional with two possible outcomes

Looks llike the code first checks to see if the user exists, if the  
user doesn’t exist, you get an error message of some sort.  
after that, user.id is compared to recipient\_id  
if they’re the same, sender, else recipient

what it’s for? who knows.  
some kind of validation and direction.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/5 "2007-03-15T16:42:44Z")

</div>

like I said it is often a difficult to read, not good choice.  
It is better to just type a little bit more and make a proper  
conditional construct in most cases.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/6 "2007-03-15T16:35:11Z")

</div>

John J. wrote:

> one of the less lovely things to read in C like languages, but after  
> you get used to it, it is actally a space saver.  
> It is relly just an conditional with two possible outcomes

In C the ?: operator is useful when writing macros since it’s an  
expression, as opposed to if/then, which is a statement. (Unlike Ruby.)  
The ?: operator has been terribly abused, though, by people who believe  
that you can make a program run faster by taking out whitespace.

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/7 "2007-03-15T16:46:45Z")

</div>

On 15.03.2007 15:54, Stefano C. wrote:

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

You got a question mark too much in there. 🙂

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

Yes.

robert

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/8 "2007-03-15T16:55:47Z")

</div>

Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> John J. wrote:
> 
> > one of the less lovely things to read in C like languages, but after  
> > you get used to it, it is actally a space saver.  
> > It is relly just an conditional with two possible outcomes
> 
> In C the ?: operator is useful when writing macros since it’s an  
> expression, as opposed to if/then, which is a statement. (Unlike  
> Ruby.) The ?: operator has been terribly abused, though, by people  
> who believe that you can make a program run faster by taking out  
> whitespace.

In effect, you are saying that ?: is the wrong way to fix a defect in  
the language.

By the way, you can do something like

#include \<stdio.h\>  
#define abs(x) ({typeof(x) \_x = x; if (\_x\<0) \_x = -\_x; \_x;})  
int main() {  
printf(“%d %f\n”, abs(5), abs(-7.3));  
return 0;  
}

with GCC (the obvious simplification using if/else as the last thing  
in the block does _not_ work, however).

---

<div class="post-metadata">

**Author:** ![jko170](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@jko170](https://www.ruby-forum.com/u/jko170)\
**Post date:** [March 15, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/what-does-this-do/88915/9 "2007-03-15T20:36:23Z")

</div>

Great explanations! Thanks guys.
