Solved

Conditional Operator

Posted on 2008-10-20
3
716 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rails installation ? 6 843
Ruby on Rails installation fail on OS X Lion 20 3,868
PostgreSQL ruby pg gem not installing 6 2,048
Iterate over classes 1 196
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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