About Ruby inheritance

Programming study diary

July 11, 2020 Progate Lv.175 RubyⅤ

Inheritance

You can create a class from scratch, but you can use an existing class and create a new class based on that class. By doing so, you can put together the common parts and write the code efficiently. Creating a new class based on a certain class is called inheritance. You can define a new class by inheriting another class with class new class name <original class name. The new class is called the "child class" and the original class is called the "parent class". The instance method of the parent class is inherited.

food.rb


require "./menu"
class Food < Menu
end

When an instance of a child class inherits, the child class inherits the instance method of the parent class.

index.rb


food1=Food.new
puts food1.name
puts food2.info

menu.rb


class Menu
  attr_accessor :name 
  #processing
  def info
    return "#{self.name} #{self.price}Circle"
  end
end

Add instance variable

Use ʻattr_accessor` as before to add instance variables to child classes.

menu.rb


class Menu
  attr_accessor :name 
  attr_accessor :price
end

Because the Food class inherits from the Menu class. It has three instance variables "name, price, calorie".

food.rb


class Food < Menu
  #Add calorie to Food class
  attr_accessor :calorie
end

Instance methods can be added in the same way.

override

You can override a method by defining a method in the child class that has the same name as the parent method. This is called overriding.

index.rb(Call the method)


food1~Food.new(...)
food1.calorie=700
puts food1.info

menu.rb(Parent class)


class Menu
  #processing
  def info
    #processing
  end
end

food.rb(Child class)


class Food < Menu
  #processing
  #Method overwrite (overwritten method is called)
  def info
    #processing
  end
end

The instance of the child class preferentially calls the method defined in the child class. Therefore, if the child class and the parent class have methods with the same name, the contents of the method of the child class are overwritten. You can also override it with the initialize method.

super You can call a method with the same name in the parent class by setting super in the overridden method. Since super calls a method, it is necessary to pass an argument to super according to the method of the parent class.

menu.rb


class Menu
  attr_accessor :name 
  attr_accessor :price
  def initialize(name:, price:)
    self.name=name
    self.price=price
  end
end

food.js


class Food < Menu
  attr_accessor :calorie
  def initialize(name:, price:, calorie:)
    super(name: name, price: price)
    self.calorie=calorie
  end
end
0711.png

Date class

A class that handles dates. Date class is a class already prepared by Ruby, and you can use it without defining a class by reading date with require. Classes already prepared have a different way of writing require than others </ font>

index.rb


# "/date"is not!
reruire "date"

Instance of Date class

You can create an instance with Date.new. You can create an instance of today's date with Date.today.

index.rb


require "date"
#Create Date method by passing "year / month / day" as an argument
date1=Date.new(2020,7,8)
puts date

console


2020-07-08

Instance method of Date class

There are many instance methods. It is the sunday? Method that passes whether it is Sunday or not as a boolean value.

index.rb


require "date"
date1=date1.new(2020,7,11)
puts date1.sunday?

console


false

Class method

A method to call on a class. The place of today of Date.today. Class methods can be defined by using def class name.method name. Unlike the instance method, write the class name before the method name. Call the class method with class name.method name.

menu.rb


class Menu
  #processing
  #Returns a boolean value indicating whether today's date is Sunday
  def Menu.is_discount_day?  
    #Date instance with today's date information
    today=Date.today
    return today.sunday
  end
end
puts Menu.is_discount_day?

Call a class method inside an instance method

menu.rb


class Menu
  #processing
  def get_total_price  #Instance method
    if Menu.discount_day?  #Calling a class method
      #processing
    end
  end
end
0711-1.png

Recommended Posts

About Ruby inheritance
About inheritance
About ruby ​​form
About class inheritance.
About Ruby Hashes
About Ruby arrays
About ruby block
About Ruby Hashes
About java inheritance
About object-oriented inheritance and about yield Ruby
About Ruby Symbols
About Ruby variables
About Ruby methods
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
About Ruby Hashes (continued)
About eval in Ruby
[ruby] About here documents
About Ruby if statement
About Ruby instance methods
About inheritance (Java Silver)
Ruby inheritance and delegation
About encapsulation and inheritance
[Ruby] About instance generation
About the [ruby] operator
Thinking about logic Ruby
Inheritance
Write class inheritance in Ruby
Inheritance
Explanation about Ruby Range object
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
Ruby About various iterative processes
About =
About Ruby and object model
About Ruby classes and instances
Explanation about Ruby String object
About the behavior of ruby Hash # ==
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
[Super Introduction] About Symbols in Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
About method.invoke
Ruby learning 4
About attr_accessor
[Ruby] Array
About Hinemos
Ruby basics
Ruby learning 5
Ruby basics
About params
About Docker
Ruby Review 2
Ruby addition
Refactoring Ruby
About Rails 6
About Spring ③
Ruby learning 3