Ruby methods and classes (basic)

Something to say

1: Method 2: Class 3: Instance method 4: Class method is.

1: What is a method?

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.

Benefits of using methods

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.

2: What is a class?

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.

Create an instance with information

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.

3: What is an instance method?

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.

4: What is a class method?

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.

reference

[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.

Recommended Posts

Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes
[Ruby] Singular methods and singular classes
[Ruby] Classes and instances
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Ruby classes and instances
Ruby variables and methods
Basic methods of Ruby hashes
Basic methods of Ruby arrays
[Java] Generics classes and generics methods
About Ruby classes and instances
Ruby variables and functions (methods)
Creating Ruby classes and instances
Java abstract methods and classes
Ruby standard input and various methods
How to call classes and methods
Java generics (defines classes and methods)
With ruby ● × Game and Othello (basic review)
Classes and instances
Ruby basic terms
Java programming (classes and instances, main methods)
Functions and methods
JAVA learning history abstract classes and methods
Ruby Learning # 15 Methods
Write code using Ruby classes and instances
About Ruby methods
Ruby: Differences between class methods and instance methods, class variables and instance variables
Symbols and Destructive Ruby
HashMap and HashSet classes
Ruby learning points (basic)
[Ruby] Big Decimal and DECIMAL
Ruby Learning # 29 Classes & Objects
About classes and instances
ruby basic syntax memo
Ruby Learning # 31 Object Methods
About Ruby instance methods
Ruby inheritance and delegation
[Ruby] methods, instance methods, etc ...
List and happy classes
ruby memorandum (basic edition)
java (classes and instances)
Basic operators and operations
Feel the basic type and reference type easily with ruby
About the difference between classes and instances in Ruby
Feel the basic type and reference type easily with ruby 2
Ruby classes are constants, not
Basic data type and reference type
How to separate words in names in classes, methods, and variables
GraphQL Ruby and actual development
(Note) Java classes / variables / methods
rails path and url methods
Ruby syntax errors and countermeasures
Ruby on Rails basic learning ①
Three Bit Manipulation Methods (Ruby)
[Ruby] Creating code using the concept of classes and instances
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
About classes and instances (evolution)
About pluck and ids methods
Consideration about classes and instances
What are Ruby class methods?