[Ruby] About Ruby classes and instances

3 minute read

Programming study diary

July 11, 2020 Progate Lv.175
RubyⅣ

Classes and instances

Classes and instances are briefly described here. In order to create a “thing” called a menu by programming, the blueprint is first required. A blueprint is called a class, and a “thing” is called an instance. (Create a “thing” (instance) based on the blueprint (class)), so to create an instance, prepare a class, create an instance from the class, and add information to the instance.

Class definition

Classes can be defined with class class name. Be sure to start the class with uppercase letters and remember to write ʻend`.

index.rb


class Menu
end

attr_accessor
An instance variable that can be directly modified and manipulated. Write ʻattr_accessor symbol` to have information. You can also use multiple instance variables for a class.

index.rb


class Menu
  attr_accessor :name
end

Instance generation

To create a new instance based on the class, use class name.new. You can assign the generated instance variable by setting variable name = class name.new.
In order to give information to the instance, assign a value to the instance variable prepared by the class. You can set a value in an instance variable with instance.variable name = value.

index.rb


class Menu
  #processing
end
#Create an instance from the Menu class and assign it to the variable menu1
menu1=Menu.new
menu1.name="Sushi"

Use methods in class

The method defined in the class is called as it is used for the instance. Call with instance.method name. A method that calls a method defined in a class is called an instance method.

index.rb


class Menu
  attr_accessor :name
  attr_accessor :price
  #Define methods in class
  def show
    puts "menu"
  end
end
menu1=Menu.new
#Calling a method defined in a class
menu1.show

console


menu

Instance method

A method to call for an instance defined in a class. It can accept arguments and return a return value. The “instance variable” that is the information that the instance has and the instance method that is called for the instance are defined in the class.

index.rb


class Menu
  #processing
  def show(data)
    return "#{data}is"
  end
end
menu1=Menu.new
puts menu1.show("menu")

console


It is a menu

Use instance variables in instance methods

Instance variables can be handled by using a special variable self in the instance method and making it self.variable name. In the instance method, the called instance itself is assigned to the variable self.

index.rb


class Menu
  #processing
  def show_name
    puts "#{self.name}is"
  end
end
menu1=Menu1.new
menu1.name="Sushi"
#Instance method call
menu1.show_name

console


It's sushi

initialize method

Processing can be executed immediately after creating an instance. Called automatically immediately after creating an instance with class name.new.

index.rb


class Menu
  #processing
  def initialize
    puts "menu"
  end
end
#Menu.Automatically call initialize method when new is executed
menu1=Menu.new

console


menu

In the instance method, you can handle the instance variable with self.variable name, and assign the value to the instance variable with self.variable name = value.

index.rb


class Menu
  #processing
  def initialize
    self.name="Sushi"
  end
end
menu1=Menu.new
puts menu1.name

console


Sushi

Variable of initialize method

You can also pass arguments to the initialize method. You can pass the value by passing an argument to class.new. Of course, keyword arguments can also be used.

index.rb


class Menu
  #processing
  def initialize(message)
    puts message
  end
end
menu1=Menu.new("Hello")

console


Hello

require
Read the file. (Used when the main statement is separated and you want to read a class in another file)

index.rb


requie "./menu"
menu1=Menu.new(name: "Sushi"

class Menu
  attr_accessor :name
  attr_accessor :price
end

gets.chomp
Used when accepting input. When this code is executed, the console will be in a state of waiting for input. By setting variable = gets.chomp, the input value can be assigned to the variable until the enter key is pressed.

index.rb


puts "Enter name"
name=gets.chomp
puts "What's your name#{name}is"

console


Enter name
Tanaka (Enter)
Your name is Tanaka

If you want to receive a number, you can convert the input to a number by doing gets.chomp.to_i.

index.rb


puts "Enter numbers"
number=gets.chomp.to_i #Convert the entered content to a number
puts "#{number}Twice#{number*2}is"