Link to home
Start Free TrialLog in
Avatar of ARACK04
ARACK04

asked on

Conditional Operator

Hi, is there a conditional operator in Ruby like the ? in C/C#, ie

x = y == nil ? 0 : 1

I know you can do

x = if (y == nil) then 0 else 1 end

Just wondering if the more compact is possible in Ruby.  

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ARACK04
ARACK04

ASKER

Huh - so I guess the above would also be

x = y.nil? ? 0 : 1

Cool - I feel like I fool for not trying it - I ASSumed since a lot of the methods ended in ?  it wouldn't also be an operator.

Thanks again.
yep,
x = y.nil? ? 0 : 1
would be the nicer, more ruby-like form