One line if statement not working

Ruby

Ruby Problem Overview


<%if @item.rigged %>Yes<%else%>No<%end%>

I was thinking of something like this?

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

But it doesn't work. Ruby has the ||= but I"m not even sure how to use that thing.

Ruby Solutions


Solution 1 - Ruby

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

Ternary operator has form condition ? if_true : if_false

Solution 2 - Ruby

In Ruby, the condition and the then part of an if expression must be separated by either an expression separator (i.e. ; or a newline) or the then keyword.

So, all of these would work:

if @item.rigged then 'Yes' else 'No' end

if @item.rigged; 'Yes' else 'No' end

if @item.rigged
  'Yes' else 'No' end

There is also a conditional operator in Ruby, but that is completely unnecessary. The conditional operator is needed in C, because it is an operator: in C, if is a statement and thus cannot return a value, so if you want to return a value, you need to use something which can return a value. And the only things in C that can return a value are functions and operators, and since it is impossible to make if a function in C, you need an operator.

In Ruby, however, if is an expression. In fact, everything is an expression in Ruby, so it already can return a value. There is no need for the conditional operator to even exist, let alone use it.

BTW: it is customary to name methods which are used to ask a question with a question mark at the end, like this:

@item.rigged?

This shows another problem with using the conditional operator in Ruby:

@item.rigged? ? 'Yes' : 'No'

It's simply hard to read with the multiple question marks that close to each other.

Solution 3 - Ruby

One line if:

<statement> if <condition>

Your case:

"Yes" if @item.rigged

"No" if !@item.rigged # or: "No" unless @item.rigged

Solution 4 - Ruby

From what I know

3 one-liners

  1. a = 10 if <condition>

example:

a = 10 if true # a = 10
b = 10 if false # b = nil

2. a = 10 unless <condition>

example:

a = 10 unless false # a = 10
b = 10 unless true # b = nil

3. a = <condition> ? <a> : <b>

example:

a = true ? 10 : 100 # a = 10
a = false ? 10 : 100 # a = 100

I hope it helps.

Solution 5 - Ruby

Both the shell and C one-line constructs work (ruby 1.9.3p429):

# Shell format
irb(main):022:0> true && "Yes" || "No"
=> "Yes"
irb(main):023:0> false && "Yes" || "No"
=> "No"

# C format
irb(main):024:0> true ? "Yes" : "No"
=> "Yes"
irb(main):025:0> false ? "Yes" : "No"
=> "No"

Solution 6 - Ruby

if else condition can be covered with ternary operator

@item.rigged? ? 'Yes' : 'No'

Solution 7 - Ruby

For simplicity, If you need to default to some value if nil you can use:

@something.nil? = "No" || "Yes"

Solution 8 - Ruby

You can use:

(@item.rigged) ? "Yes" : "No"

If @item.rigged is true, it will return 'Yes' else it will return 'No'.

Solution 9 - Ruby

if apple_stock > 1
  :eat_apple
else
  :buy_apple
end

Above statement can written in form of ternerary statement in ruby as:

apple_stock > 1 ? :eat_apple : :buy_apple

Solution 10 - Ruby

Don't forget it can generate the content from the ruby tag:

  v
<%= @item.rigged? ? "Yes" : "No" %>

The tick (v) indicates using the equal sign to generate text in the html.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoRView Question on Stackoverflow
Solution 1 - RubyNikita RybakView Answer on Stackoverflow
Solution 2 - RubyJörg W MittagView Answer on Stackoverflow
Solution 3 - RubyBakir JusufbegovicView Answer on Stackoverflow
Solution 4 - RubyAlan DongView Answer on Stackoverflow
Solution 5 - RubymarkView Answer on Stackoverflow
Solution 6 - Rubynitanshu vermaView Answer on Stackoverflow
Solution 7 - RubyaabiroView Answer on Stackoverflow
Solution 8 - RubySnm MauryaView Answer on Stackoverflow
Solution 9 - RubyShubham kumarView Answer on Stackoverflow
Solution 10 - RubydirktayView Answer on Stackoverflow