Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line if statement not working

Tags:

ruby

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

like image 783
RoR Avatar asked Sep 30 '10 05:09

RoR


People also ask

How do you write an if statement with one line?

How to Write If Without Else in One Line? We've already seen an example above: we simply write the if statement in one line without using the ternary operator: if 42 in range(100): print("42") . Python is perfectly able to understand a simple if statement without an else branch in a single line of code.

Are one line if statements OK?

Only use single-line if statements on a single line While the compiler sees this as one statement guarded by a single condition, humans often accidentally read this is an if block, whether there are curly braces or not, thanks to the indentation. Humans notice the indentation, the compiler does not.

How do you write an if statement in one line JavaScript?

Writing a one-line if-else statement in JavaScript is possible by using the ternary operator. Running this code prints “Adult” into the console. This code works fine. But you can make it shorter by using the ternary operator.

Can we write if else into one line in Python?

Python If Statement In One Line In Python, we can write “if” statements, “if-else” statements and “elif” statements in one line without worrying about the indentation. In Python, it is permissible to write the above block in one line, which is similar to the above block.


1 Answers

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

Ternary operator has form condition ? if_true : if_false

like image 194
Nikita Rybak Avatar answered Sep 21 '22 21:09

Nikita Rybak