One Line If Statement in Ruby

One line if statement not working

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

Ternary operator has form condition ? if_true : if_false

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 in Ruby

(day -= 31; month = "April") if day > 31

Alternate way (As suggested by @mudasobwa in comments below) :

day, month = day - 31, "April" if day > 31

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"

Using a one-line unless or if statement in Ruby

I'm not sure what exactly you want but what I understood is you want to display instrument.year or N/A if instrument.year is an empty string.

So what I would do is use a ternary:

puts "Year: ".colorize(:light_blue) + "#{instrument.year.empty? ? 'N/A' : instrument.year}"

Should I split a ruby single-line if statement into multi-line if statement because the line is long?

Style questions aside, if you want to maintain your current semantics, you can break lines at certain keywords and operators without escaping newlines with backslashes. For example:

address =
Module::InnerModule::Class.new(long_address) if
Module::Class.new(long_address).is_good?

Otherwise, change your semantics or refactor your code to fit your desired line length and chosen style. Questions about how to split lines are answerable, but the “best” way to split, indent, or refactor are largely subjective, and mostly amount to a combination of readability and intent.

One-line `if` return statement with two actions

  1. When you have a method like return, break, etc., you can do (assuming that you are not interested in the return value):

    return log("some message") if var1.nil?
  2. A naive general way is:

    (log("some message"); return) if var1.nil?
  3. But if you don't like the parentheses and semicolon, then, do this: If and does not work, then that means the return value of the first expression is falsy, which means or should work.

    log("some message") or return if var1.nil?

    In general, one or the other between and and or would work, depending on the evaluated value of the first expression.

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 one line if statement with exception handling

If you have active_support available, you can use Object#try:

a.try(:b).try(:c) or 'Fill here'

If you don't have that, it's pretty easy to monkey-patch Object to add one. Here's the code in active_support, just put it some where before you are using try method.

class Object
def try(*a, &b)
if a.empty? && block_given?
yield self
else
public_send(*a, &b) if respond_to?(a.first)
end
end
end

After that, you can use it:

a = nil
a.try(:b).try(:c).try(:nil?) #=> true

b = 1
b.try(:+, 2) #=> 3

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 %>


Related Topics



Leave a reply



Submit