What does this do? :

S

Stefano Crocco

Alle gioved=EC 15 marzo 2007, jko170 ha scritto:
Looking at this piece of code, what does the colon do?

def partner_to(user)
raise ArgumentError unless user
user.id =3D=3D recipient_id ? sender : recipient
end

I got the code from this rails post:
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/7fdc= cd
0b9e0ce

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

This is a shortcut for
if user.id =3D=3D recipient_id ? then sender
else recipient
end

What comes before the ? is the condition (not to confuse with the ? which e=
nds=20
the name of predicate methods: array.empty? do_something : do_something_els=
e=20
won't work, array.empty? ? do_something : do_something_else will), after=20
the ? and before the : there's the action to take if the condition is true=
=20
and after the : what to do if it's false.

Stefano
 
J

John Joyce

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

Tim Hunter

John said:
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.
 
R

Robert Klemme

Alle giovedì 15 marzo 2007, jko170 ha scritto:

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
 
J

John Joyce

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

David Kastrup

Tim Hunter said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top