I tried to understand nil guard

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.

nil guard

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.

Summary

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.

reference

・ Ruby on rails 5 that can be used in the field ・ Https://www.sejuku.net/blog/19044

Recommended Posts

I tried to understand nil guard
I tried to verify yum-cron
I tried to chew C # (indexer)
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
I tried to summarize the methods used
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried to solve AOJ's Binary Search
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to build AdoptOpenjdk 11 on CentOS 7
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to use Selenium like JQuery
I tried to touch JavaScript Part.2 Object-oriented
I tried to implement ModanShogi with Kinx
I tried Spring.
I tried tomcat
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
I tried to summarize about JVM / garbage collection
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to implement polymorphic related in Nogizaka.
I tried to manage struts configuration with Coggle
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to manage login information with JMX
I tried to organize the session in Rails
java I tried to break a simple block
I tried to link grafana and postgres [docker-compose]
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried to chew C # (basic of encapsulation)
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried to create a LINE clone app
I tried to build AdoptOpenJDK 8 (Addition: Amazon Corretto 8)
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to output multiplication table in Java
I tried to link JavaFX and Spring Framework.
I tried to set tomcat to run the Servlet.
I tried to build Micra mackerel in 1 hour!
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
[JavaScript] The strongest case when I tried to summarize the parts I do not understand
I tried what I wanted to try with Stream softly.
[I tried] Spring tutorial
I tried to implement file upload with Spring MVC
I tried to organize the cases used in programming