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:
- No.
- 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
Can't Install Rmagick, Pkg-Config: Command Not Found
How to Setup Urls for Static Site with Ruby Rack on Heroku
Rake Db:Migrate Error with MySQL2 Gem - Library Not Loaded: Libssl.1.0.0.Dylib
Jekyll: How to Use Custom Plugins with Github Pages
Named Parameters in Ruby Structs
Why Is Date.Today - 6.Months + 6.Months != Date.Today
No Such File to Load Bundler Error for Rails 3
How to Create Md5 Hash with Hmac Module in Ruby
Natural Language Date Parser for Ruby/Rails
Removing Child Root Nodes in Rabl
Detect Browser Language in Rails
How to Use Ajax Send Data to Controller in Ruby on Rails
Ruby: Creating a Hash Key and Value from a Variable in Ruby
How to Start the Ruby Debugger on Exception