hguser
hguser

Reputation: 36058

What is the counterpart for expression `?value1:value2` in ruby?

I want to know what the counterpart is in Ruby for this kind of expression:

var status=false;
var xx=new Obj(xx,status?"0":"1",status?"2":"3");

I tried the same in Ruby, but it seems that the syntax:

status?"23":nil

does not work.

Upvotes: 1

Views: 85

Answers (2)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230461

Put a space between status and ?. Seems that it might get parsed as a method name status?. Also, don't terminate your sentences with semicolons. And don't use var.

x = status ? "0" : "1"

Upvotes: 2

David Grayson
David Grayson

Reputation: 87486

Method names can end with question marks, so use more spaces:

status ? "23" : nil

Equivalently you could write:

("23" if status)

Upvotes: 3

Related Questions