Sometimes, you want to initialize a variable to some value only if that variable is equal to nil. If you like to write your methods as short, clean and readable as possible, you can do that task in a single line instead of taking the traditionnal 3 lines approach. You have two options :
1) You can use the if modifier
x = get_some_object if x.nil?
Very easy to read. This is my favorite.
2) Or you can use the ||= operator
x ||= get_some_object
This one is even shorter. The ||= operator is a little bit less verbose and might confuse a ruby newcomer… but I’m not saying that it should be a reason for not using it. This is more a matter of personal taste than anything else.
Both of these methods are better than this :
#eeww... we don't like this one. if(x.nil?) x = get_some_object end