While studying Ruby, a nil guard came out, so I wrote an article as a record of my study.
Please point out if there is a misunderstanding.
In Ruby, there is a way to write nil guard to prevent the state of becoming nil. The simple explanation is that if the variable is nil, enter a value.
So I would like to give an example below for easy understanding.
#Example 1 > number = nil > number ||= 6 > puts number => 6 #Example 2 > number = 5 > number ||= 6 > puts number => 5
Pipe operator to first implement nil guard||When=Combine. In Example 1, if number is nil, 6 is assigned. However, in Example 2, the number is not nil, so no assignment is made. Here, it turns out that the nil guard is not assigned unless it is nil or false.
The nil guard is very useful in situations where a variable may contain nil and you want to put some default value in place of nil.
・ Ruby on rails 5 that can be used in the field ・ Https://www.sejuku.net/blog/19044