[RUBY] I want to be aware of the contents of variables!

language

Ruby

Target audience

--Those who are not aware of variable types. --Those who have just studied programming --Those who do not understand variables ――When NoMethodError occurs in Ruby, I can't think of the cause.

Article content

We do not explain difficult parts of variables and types (reference type, value type, null, scope, etc.), but create it so that you can grasp the image of the variable.

At least what you want to know

Be aware of the contents of variables!

Only this. This is as important as it is static or dynamic. The image is like this.

スクリーンショット

Programmers who are unaware of the contents of variables often write this way.

Variable! Please cook! !!

スクリーンショット 2020-12-23 16.27.25.png

You don't know that the contents of the variable are carpenters. So I tell the carpenter to cook. The code is as follows.

#Defines a carpenter class.
class Carpenter
#Defines a construction method.
  def construction
    puts "To build"
  end
end
#It materializes the carpenter class.
hennsuu = Carpenter.new
#You are calling a method that is not defined in the carpenter class.
hennsuu.cooking

Do you understand? I will explain it again using a class that actually exists. See the official documentation for the String and Integer classes in Ruby.

Search for upcase in the site (command + F). A String will hit, but an Integer will not. This means that the String class has an upcase method defined, but the Integer class does not have an upcase method defined. Now let's actually execute the code.

hennsuu = "abcdef"
puts hennsuu.upcase #"ABCDEF"
hennsuu2 = 12345
puts hennsuu2.upcase #NoMethodError
hennsuu.class # String
hennsuu.class # Integer

You got a NoMethod error. This is an error because the upcase method is not defined in the Integer class. In this way, you cannot write a program without being aware of the contents of variables, or more specifically, without being aware of the classes of the contents of variables.

Summary

You may suffer from unexpected bugs and errors if you are not aware of the contents of the variables. Be aware of the contents of variables!

External site

The cute picture used this time was obtained from the following site. The copyright of the material is held by Mr. Takashi Mifune. https://www.irasutoya.com/

Recommended Posts

I want to be aware of the contents of variables!
I want to var_dump the contents of the intent
I want to output the day of the week
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to return the scroll position of UITableView!
I want to see the contents of Request without saying four or five
[Ruby] Display the contents of variables
I want to set the conditions to be displayed in collection_check_boxes
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to narrow down the display of docker ps
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
I want to control the default error message of Spring Boot
I want to recreate the contents of assets from scratch in the environment built with capistrano
I want to change the value of Attribute in Selenium of Ruby
A collection of patterns that you want to be aware of so as not to complicate the code
When you want to check whether the contents of a property can be converted to a specific type
I want to display the number of orders for today using datetime.
I want to know the JSP of the open portlet when developing Liferay
[Ruby] I want to extract only the value of the hash and only the key
I want to get the field name of the [Java] field. (Old tale tone)
I want you to use Enum # name () for the Key of SharedPreference
After all I wanted to preview the contents of mysql with Docker ...
Things to be aware of when writing Java
Customize how to divide the contents of Recyclerview
I want to be eventually even in kotlin
I want to truncate after the decimal point
I want to get the value in Ruby
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
I want to separate the handling of call results according to the API caller (call trigger)
I managed to get a blank when I brought the contents of Beans to the textarea
I want to recursively get the superclass and interface of a certain class
I want to call a method of another class
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to dark mode with the SWT app
Things to be aware of when using devise's lockable
I want to call the main method using reflection
I am keenly aware of the convenience of graphql-code-generator, part 2
[Rails] How to get the contents of strong parameters
Java: Use Stream to sort the contents of the collection
[Rough commentary] I want to marry the pluck method
[Java] Things to be aware of when outputting FizzBuzz
I want to simplify the log output on Android
I want to add a delete function to the comment function
[Ruby] I want to make a program that displays today's day of the week!
Rails The concept of view componentization of Rails that I want to convey to those who want to quit
[Rails] I want to display the link destination of link_to in a separate tab
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...