Is There a Shorthand If (Without Else) Statement in Ruby on Rails

Is there a shorthand if (without else) statement in Ruby on Rails?

You can use post conditions (don't mind the name, it will be evaluated before the code. And do_something will only be executed if condition evaluates to truthy value (i.e. not nil or false)).

do_something if a

How to use if (without else) statement for multiple lines of code?

This is very basic ruby syntax. All the ruby control structures can be used in inline way, or in multi-line/block way, closed with end keyword.

def self.foo(a)
if a == true
# mutiple lines of code
end
end

For more informations about syntax and ruby best practices, you can refer to : this ruby style guide

Ruby ternary operator without else

a.action if object.method?

Rails If- Elseif- Else shorthand

you can try this:

(a == b) ? do_something(a) : (a == c) ? do_something(c) : do_something(b)

Or

(a == b) ? do_something(a) : ((a == c) ? do_something(c) : do_something(b))

Ruby short hand for simple if else condition

This pattern is what the or-operator is for.

@canonical_url || request.original_url

Or, in cases where the first branch isn't just the result if the test, the conditional operator works as well:

some_condition ? @canonical_url : request.original_url

Rails shorthand if/else output html

Try using the html_safe method.

<%= @inbox.automatic_reconciliation ? "<i class='fi-play-circle'></i>".html_safe : "<i class='fi-pause'></i>".html_safe %>

documentation

One line if statement not working

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

Ternary operator has form condition ? if_true : if_false

Shortcut for if..else..end

A common Ruby idiom to achieve this is:

def test
!!get_from_somewhere
end

The double bang turns an object into its "boolean equivalent":

object = 'foo'
!object
# => false
!!object
# => true

Pay attention that in Ruby, unlike Python, just false and nil evaluates to false in boolean context, for example:

!!0
# => true

Why does using the shorthand if syntax does not evaluate when searching for a sub-string using include?

It's a precedence problem.

Solution

You need :

puts "test".include?("s") ? "yep" : "nope"
#=> yep

Why?

Method call without parenthesis is somewhere between defined? and or in the precedence table, so it is lower than the ternary operator. It means that

puts "test".include? "s" ? "yep" : "nope"

is parsed as

puts "test".include?("s" ? "yep" : "nope")

which is

puts "test".include?("yep")

which is

false

Warning

"s" ? "yep" : "nope"

displays a warning :

warning: string literal in condition

because ternary operator expects a boolean, and a String is always truthy.

1 > 2

The reason this works

puts 1>2 ? "1 is greater than 2" : "1 is not greater than 2"

is that the ternary operator has a higher precedence than puts :

puts ( 1>2 ? "1 is greater than 2" : "1 is not greater than 2" )

It is evaluated as :

puts ( "1 is not greater than 2" )

One last tip

When you have precedence problem, using puts without parenthesis might just make the problem worse. You can fire up IRB and see what the result is directly.

Here's an example :

# in a script :
puts [1,2,3].map do |i|
i * 2
end
#=> #<Enumerator:0x0000000214d708>

With IRB :

[1,2,3].map do |i|
i * 2
end
# => [2, 4, 6]

Ruby - is there a shorthand check for two logical conditionals against one variable

unless ['Beck', 'Led Zeppelin'].include?(artist)
5.times { puts 'sorry' }
end

Isn't any "shorter", but no obscure syntax trickery too. Just using regular array api. As a consequence, you can provide that array in any way you want. Load it from a file, for example. With any number of elements.



Related Topics



Leave a reply



Submit