[Ruby] How to prevent errors when nil is included in the operation

I get an error if the formula contains nil

For example, if you try to output such a simple calculation result (@x below), if nil is included, you should get some error.

a = 1
b = nil
@x = a + b

Countermeasures

Use the .to_i method. to_i is a method that converts a string to an integer (if it can be recognized as an integer). If it is not recognized as an integer, it returns 0.

Even in the case of nil, since the integer is not recognized, 0 is returned and no error is issued.

If you want to output @x above,

@x.to_i

It's OK if you write!

Recommended Posts

[Ruby] How to prevent errors when nil is included in the operation
How to output the value when there is an array in the array
How to write the view when Vue is introduced in Rails?
How to build the simplest blockchain in Ruby
How to resolve errors that occur in the "Ruby on Rails" integration test
How to set when "The constructor Empty () is not visible" occurs in junit
How to set chrony when the time shifts in CentOS7
How to retrieve the hash value in an array in Ruby
How to perform a specific process when the back button is pressed in Android Fragment
How to solve the problem when the value is not sent when the form is disabled in rails and sent
How to install Bootstrap in Ruby
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to solve the unknown error when using slf4j in Java
How to get date data in Ruby
How to resolve errors when installing Rails 5.1.3
How to get the date in java
[Docker] How to build when the source code is bind-mounted on the container
How to constrain the action of the transition destination when not logged in
How to debug the processing in the Ruby on Rails model only on the console
How to reference a column when overriding the column name method in ActiveRecord
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
What to do if Operation not permitted is displayed when you execute a command in the terminal
[swift] How to control the behavior when the back button of NavigationBar is pressed
Possibility when deploying to EC2 but nothing is displayed in the error log
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
How to check the logs in the Docker container
Behavior when wild card (**) is specified in ruby
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
If the parameter is an array, how to include it in Stopara's params.permit
The ruby version is managed in the .rbenv / version file
How to check if an instance variable is defined in a Ruby class
When the server fails to start in Eclipse
How to prevent direct URL typing in Rails
How to add sound in the app (swift)
How to delete the database when recreating the application
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[Ruby on Rails] How to install Bootstrap in Rails
How to log in automatically when Ubuntu restarts
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
How to check Rails commands in the terminal
When the project is not displayed in eclipse
How to implement Pagination in GraphQL (for ruby)
I want to get the value in Ruby
[Ruby basics] How to use the slice method
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to fix the problem that the upper half is cut off when using UITabBar
How to identify the cause when GC occurs frequently and CPU usage is high
Summary of how to use the proxy set in IE when connecting with Java
[Ruby] How to use standard output in conditional branching
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
How far is the correct answer to divide the process?
[Ruby on Rails] How to write enum in Japanese
[Ruby] How to find the sum of each digit
[Ruby on Rails] How to change the column name
Organized how to interact with the JDK in stages