Ruby If .. Elsif .. Else on a Single Line

Ruby if .. elsIf .. else on a single line?

a = (foo && "a" or bar && "b" or "c")

or

a = ("a" if foo) || ("b" if bar) || "c"

Ruby: One-Line If-Then-Else

The two one liners that exist are the two you already described:

Ternary:

a ? b : c

if-struct:

if a then b else c end

So to answer your questions:

  1. No.
  2. The ternary is preferred according to this, this,
    while the if-struct is preferred according to this, and this. Ruby does not have an official style guide, you won't find a concrete answer I'm afraid. Personally, I use the if-struct, as it reduces cognitive load, if being more verbose.

However, all the most of the style guides say ternary operators are fine for simple constructs:

Avoid the ternary operator (?:) except in cases where all expressions are extremely trivial.

One line if statement not working

Remove if from if @item.rigged ? "Yes" : "No"

Ternary operator has form condition ? if_true : if_false

Rails single line if else statement

To make it even clearer, you can use logical OR and ActiveSupport's Object#presence (to put collection.name only if it exists and isn't blank):

<%= collection.name.presence || @miniature.name %>

If you want to display collection.name if it's not nil, but it's blank (empty string or string containing only whitespace), it will be enough to have:

<%= collection.name || @miniature.name %>

Ruby - Using multiple conditions on a single line

This should do it:

if letters.eql?(letters.upcase) && dash.eql?('-') && numbers.to_i.to_s.eql?(numbers)

You can still wrap the entire conditional in parenthesis if you would like, but with Ruby (unlike JavaScript), you don't need to.

Ruby multiple statements on single line with condition

You could use :

string = "dasff1"

case string
when "dasff1" then print "dasff1"; print " hoiff1"
when "dasff2" then print "dasff2"; print " hoiff2"
when "dasff3" then print "dasff3"; print " hoiff3"
end

Or just :

print string+" "+string.sub('das','hoi') if string=~/^dasff[1-3]$/

You shouldn't do that, but since you asked for it :

if    string == "dasff1" then print "dasff1" ; print " hoiff1"
elsif string == "dasff2" then print "dasff2" ; print " hoiff2"
elsif string == "dasff3" then print "dasff3" ; print " hoiff3"
end

ternary operator based on if else

For something like this you might want to use a simple look-up table to eliminate some of the logic:

EQUIVALENT = {
'Y' => 'guest',
'N' => 'test'
}

if (a.present?)
b = EQUIVALENT[b.value] || b
end

The || b part may not be necessary if non-mapped b values are ignored.

Can I use `else if` over `elsif`?

You can use else if and it's safe. However note that this means extra end keywords are needed.

if n == 1
puts "foo"
elsif n == 2
puts "bar"
end

is logically the same as:

if n == 1
puts "foo"
else if n == 2
puts "bar"
end
end

or the equivalent:

if n == 1
puts "foo"
else
if n == 2
puts "bar"
end
end

Ruby if elsif else problems

It's returning the name you pass in because you're returning the str at the end of the method. So it runs through your if/else conditional and puts the string you want, but then you return the string at the end.



Related Topics



Leave a reply



Submit