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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yep,
x = y.nil? ? 0 : 1
would be the nicer, more ruby-like form
x = y.nil? ? 0 : 1
would be the nicer, more ruby-like form
ASKER
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.