Basics of Ruby ~ Review of confusing parts ~

object

An object is data handled by Ruby. String objects handle characters. Enclose a character in double quotes or single quotes to make it a string. In other words, it is called a string object. In addition to string objects, there are also time objects, date objects, array objects, numeric objects, and so on.

Object name What to handle
String object letter
Array object Multiple data
Numeric object Numerical value
Time object time
Date object date

Method

A method is a group of instructions that do something in programming. For example, if you want to output a string object to the terminal, use the puts method.

There are also methods that transform an object into another object. For example, the length method. This method is a method that can be used for string methods and converts string methods to numeric methods for their own number of characters. The to_s method converts a numeric method to a string method.

Return value

The return value is the final value after the object or method has been processed. Ruby objects themselves, expressions that use methods always have a return value.

In other words, when the length method is used, the numerical value of the number of characters in the string is returned as the return value. The to_s method converts a numeric object to a string object and returns it as a return value. The to_i method converts a string object to a numeric object.

--The return value of a single object returns the object itself. --The calculation formula returns the answer of that formula as a return value. --When a method is used, the execution result of that process is returned as a return value. --But the puts method returns nil as the return value. It means empty. The output and return values are different. --In the substitution expression, the stored value (assigned value) itself is the return value.

The gets method returns a string object with the value entered by the user as the return value. The gets method will return as a return value with a newline at the end. It is in the state with \ n in backslash notation. Use the chomp method to get rid of it. Using this method will return a string object with the new \ n removed.

Comparison operator

== Returns true as the return value when the left and right expressions and values are equal. Returns false if they are not equal. On the contrary ! = Returns true if the left and right values are not equal, false if they are equal.

variable

A variable is like a container for an object, which has a name. You can store objects in variables. Variables can be reassigned and their values can be updated. On the other hand, constants are prohibited from being reassigned in principle, and it is customary to write them in all capital letters. In other words, use a constant when you want to store a fixed value.

hash

A hash is one of the variables and is an object that can have multiple data. The value is managed by the key. The format for saving in this pair is called a key-value store. A string object and a symbol object can be used as the key of the hash object. Symbol objects are recommended because they run faster than symbol objects. A symbol object is like a label for identifying a name and can be used almost synonymously with a character string object.

index.rb


hash = {}         ##Generate an empty hash

hash[:name] = "taro"  ##Value assignment
hash[:age] = 20     ##Value assignment

puts hash[:name]     ##Get value

Self-defined method

You can define your own processes like puts and make them methods. You don't have to write the same code over and over, and the code is more readable. Also. It will be easier to modify and manage.

For the definition, write an arbitrary method name next to def and describe the processing executed until end.

index.rb


def my_name
 puts "My name is Taro."
end

It should be noted that the definition part of a method is not loaded until the method is called. Therefore, the definition part is passed through before the method name is called.

index.rb


def my_name         ##Then read here. Until then, through.
 puts "My name is Taro."
end

my_name                   ##Method call: This is read first

while statement

A grammar that allows you to repeat the same process.

index.rb


while conditional expression do
#processing
end

The process is repeated many times as long as the conditional expression part is true.

In other words

index.rb


while true do
#processing
end

Then you can do it without terminating the program in an infinite loop.

You can use the exit method in the process to stop the infinite loop at a specific time.

Array

An array is an array object. Arrays are managed in order, whereas hashes manage objects by key. The array can also contain hashes. Since the order management starts from 0, the first element is managed by 0. Use the << method to add elements to an array object. If you want to count the number of arrays, use the length method.

Variable scope

A variable has a fixed range in which the variable can be used. Usually in a method. You can only use the variables defined within that method. Use arguments to use variables defined outside the method.

argument

The argument written in the part that calls the method is called this argument. Also, the argument written in the part that defines the method is called a formal argument. This argument and the variable you want to call must have the same name, but this argument and the formal argument do not have to be the same. You can use variables in the method with the name set by the formal argument.

index.rb


def age_sister(age)

puts "my sister is #{age} years old."
return age     ##Since the puts method returns nil as the return value, the value of the variable is returned as the return value in the return statement.

end

sister = 25    ##age_The variable sister is defined outside the scope of the sister method.
age_sister_and_me(sister)    ##age_The variable sister is taken as this argument in the part that calls the sister method.

Since the return value of the return statement is fixed there, the processing ends there. The return value of the defined method is the value of the last described expression.

index.rb


def animal_name(name)
 puts "This animal#{name}is."
 return "I#{name}!"
end

puts animal_name("dog")

In the above case, since the return statement is used in the animal_name method, "I am a dog!" Is returned as the return value. With the puts method I'm a dog! Is output. The ("dog") defined on the caller of the method can be used as a variable within the method. In this case, it can be used as a variable name.

Arguments are used to determine what to output when calling a function. The method returns something when you pass an argument. When the method receives an argument, its name can be anything. At the end of the method, use return to return a value to the method caller.

each method

Each method is a method for repetition. If you use the each method on an array object, iterates as many times as there are elements in the array.

Recommended Posts

Basics of Ruby ~ Review of confusing parts ~
Ruby Basics 2 ~ Review of confusing parts ~
Basics of Ruby
Review of Ruby basic grammar
Ruby basics
Ruby basics
Basics of sending Gmail in Ruby
Ruby Review 2
Extraction of "ruby" double hash * Review
[Ruby] Review about nesting of each
Ruby basics
Ruby Review 1
[Ruby] Summary of class definitions. Master the basics.
Ruby on Rails ~ Basics of MVC and Router ~
NIO.2 review of java
Review of java Shilber
[Ruby] Exception handling basics
Arrangement of screen parts
Ruby on Rails basics
Basics of try-with-resources statement
Ruby cherry book review
NIO review of java
[Ruby] Class nesting, inheritance, and the basics of self
Review the basic knowledge of ruby that is often forgotten
Basic methods of Ruby hashes
Docker monitoring-explaining the basics of basics-
Basic methods of Ruby arrays
[GCD] Basics of DispatchQueue class
[Ruby] Various types of each
The basics of Swift's TableView
Summary of Java language basics
[Ruby] List of basic commands
Judgment of fractions in Ruby