1: Method 2: Class 3: Instance method 4: Class method is.
A method is a method that combines multiple processes.
greet.rb
def greet(name)
puts "#{name}, HELLO!"
end
greet('Alice')
> "Alice, HELLO"
** greet (name) ** corresponds to the method. The method is not loaded at the defined part (def ~ end), but only when it is executed (greet ('Alice')).
The method is 1: You can pass the value 2: The evaluation value is returned at the end 3: Variables defined in the method cannot be accessed externally It has the feature.
If the method name is long, it is customary to write it in the snake case, so
** greet_her ** instead of greetHer ** send_mail ** instead of sendMail
It is necessary to write in the form of.
1: The process defined by the method can be reused 2: Code modification becomes easier
For example, suppose you have a code like this.
number.rb
puts 1
puts 2
puts 3
puts 4
puts 1
puts 2
puts 3
puts 4
It is very troublesome to write puts 1 one by one, so I will rewrite it using a method
number.rb
def number
puts 1
puts 2
puts 3
puts 4
end
number #1 in one run,2,3,4 Outputs at once
> 1
> 2
> 3
> 4
Also, if you no longer need puts 3, you can just delete puts 3 in the method.
number.rb
def number
puts 1
puts 2
#Delete
puts 4
end
number #1 in one run,2,4 Outputs at once
> 1
> 2
> 4
By creating a method so that it can be easily reused in this way, it will be easier to modify the entire method.
The source to use when creating an instance. It might be easier to imagine if you think of it as a mold for making cookies.
cookie.rb
Class Cookie #name of the class=Cookie
def cookie #Method name=cookie
end
end
#Actually create an instance from the type called Cookie class ↓
cookie = Cookie.new
cookie
#It was done ↓
=> #<Cookie:0x00007ff34c847fa0>
<Cookie:0x00007ff34c847fa0> Is an instance created by the cookie class. Instance can be created by using class name .new. An instance refers to the entity itself created from the source. Simply put, a real cookie made from a mold.
By using the initialize method, you can have information when you create an instance. This time, let's give the cookie instance two pieces of information, size and taste.
cookie.rb
class Cookie
def initialize(size, taste)
@size = size
@taste = taste
end
end
cookie = Cookie.new('Large', 'Sweet')
cookie
=> #<Cookie:0x00007ff34d2a7108 @size="Large", @taste="Sweet">
This allowed us to generate large, sweet-tasting, cookie instances. Suddenly came out,
@size @taste
These guys starting with @ are called ** instance variables **, and you can change the data held for each instance (for each cookie). This time, size is the first argument and taste is the second argument, which correspond to'Large'and'Sweet', respectively. By applying this,
cookie.rb
cookie1 = Cookie.new('Medium', 'Bitter')
cookie1
=> #<Cookie:0x00007ff34d2cdd30 @size="Medium", @taste="Bitter">
cookie2 = Cookie.new('Small', 'Salty')
cookie2
=> #<Cookie:0x00007ff34d2cdd30 @size="Small", @taste="Salty">
And you can create cookie instances with multiple sizes and tastes.
A method that can be executed using the created instance (cookie). At the moment, there are three types of cookies: Small Salty, Medium Bitter, and Large Sweet. These are only made from the cookie class ** and are not used. ** ** Instance methods are intended to do something with these guys.
cookie.rb
cookie2
=> #<Cookie:0x00007ff34d2cdd30 @size="Small", @taste="Salty">
def cookie_taste
puts "#{@taste}"
end
=> :cookie_taste
cookie.cookie_taste
> Salty
The data of the @taste instance method of the cookie has succeeded in passing the data to @taste of cookie_taste. In this way, the instance method can refer to the data based on the instance data.
It used to be via an instance, but you can do the same via a class. Use when you don't need to refer to the instance data.
cookie.rb
class Cookie
# @@Those with two are called class variables and can be shared by all instances.
@@my_cookie_is = 0
def initialize(size, taste)
@size = size
@taste = taste
@@my_cookie_is += 1
end
#Instance method
def show_info
puts "#{@taste}Cookies#{@size}There is only size."
end
#Class method
def self.show_all_cookie
puts "I am#{@@my_cookie_is}I'll eat a piece of cookies."
end
end
After doing this
cookie.rb
#Execution of class method
cookie4 = Cookie.new('Medium','Bitter')
cookie4
=> #<Cookie:0x00007ff34d1ae440 @size="Medium", @taste="Bitter">
Cookie.show_all_cookie
I eat one cookie.
#Execution of class method
cookie5 = Cookie.new('Large','Salty')
cookie5
=> #<Cookie:0x00007ff34c851c30 @size="Large", @taste="Salty">
Cookie.show_all_cookie
I eat two cookies.
#Execution of instance method
cookie4.show_info
Bitter cookies are only medium size.
#Execution of instance method
cookie5.show_info
Salty cookies are only available in Large size.
Will be.
[Ruby] Object-oriented class concept understood by Taiyaki https://pikawaka.com/ruby/class#%E3%82%AF%E3%83%A9%E3%82%B9%E3%81%AE%E4%BD%9C%E3%82%8A%E6%96%B9 I was very helpful. I wrote it almost as a concise version of this article. By making an article, I was able to cover what was shallow knowledge.