[Ruby] postfix if

I learned about postfix if in the reference book, so I wrote an article to understand it.

Postfix if

If and unless can be placed not only at the beginning of the line, but also after the phrase you want to evaluate when the condition is met. This way of writing is called "postfix if".

Click here for if https://qiita.com/muffin-men/items/fd8637bb1b98cb369c5b

Click here for unless https://qiita.com/muffin-men/items/9d3ab806634624f5f29f

#This will be output
puts 'Good morning' if true

#This is not output
puts 'Thank you for your support' if false

Summary

If you use a postfix if, you can only describe the processing for one line, and you cannot write else. If the process spans multiple lines or you want to write other conditions such as else, use a normal if. Postfix ifs are often used in simple cases where the process is only one line.

Reference book

・ Ruby on rails 5 that can be used in the field

Recommended Posts

[Ruby] postfix if
[Ruby] present/blank method and postfix if.
Ruby if, case
Ruby Learning # 17 If Statements
About Ruby if statement
Ruby Learning # 18 If Statements (Con't)
[Ruby] problem with if statement
[Ruby] conditional branch if statement
[Ruby] If and else problems-with operators-
Ruby learning 4
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
[Ruby] Exception handling, give back if given
Ruby learning 3
Ruby setting 2
[Ruby] if statement concept of conditional expression
Ruby problem ⑦
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
if statement
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
Ruby conditional branch. if, conditional operator (ternary operator), unless, case
[Ruby] Assign variables in conditional expressions in if statements.
[Ruby] What if you put each statement in each statement? ??