Solved

Conditional Operator

Posted on 2008-10-20
3
712 Views
Last Modified: 2013-11-13
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!
0
Comment
Question by:ARACK04
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22760863
you can use the ? in ruby as in C

x = y == nil ? 0 : 1

will work
0
 

Author Comment

by:ARACK04
ID: 22760889
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.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22760914
yep,
x = y.nil? ? 0 : 1
would be the nicer, more ruby-like form
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now