Summary of contents related to class definition of ruby.
table of contents
python
class Class name
end
└ Class name is capitalized at the beginning └ No need for ":" at the end of the first line └ "end" is required at the end of the sentence
A variable that stores a value that is unique to each instance.
ʻAttr_accessor: instance variable name ` └ Space immediately after attr_accessor └ Symbol (: value) after the space └ No space between ":" and "value"
Definition of instance variables
class Product
attr_accessor :name
attr_accessor :price
end
Define instance variables "name" and "price".
Class name.new
python
class Product
attr_accessor :name
attr_accessor :price
end
#Instance generation
product1 = Product.new
Instance name. Instance variable name = value
python
class Product
attr_accessor :name
attr_accessor :price
end
#Instance generation
product1 = Product.new
#Set a value in an instance variable
product1.name = "iphone"
product1.price = 100000
puts product1.name
puts product1.price
#output
iphone
100000
Set iphone to name of the instance called product1. Set 100000 to price.
The methods in the class are called instance methods.
python
class class name
def method name(Argument name)
processing
end
end
instance name.method name
Same as calling an instance variable.
python
class Product
attr_accessor :name
attr_accessor :price
def create
return "Created an instance(return)"
end
end
#Instance generation
product1 = Product.new
#Instance method call
puts product1.create
#output
Created an instance(return)
self. Instance variable name
python
class Product
attr_accessor :name
attr_accessor :price
def create
puts "The product name is#{self.name}is"
puts "the price is#{self.price}is"
end
end
#Instance generation
product1 = Product.new
#Set a value in an instance variable
product1.name = "iphone"
product1.price = 100000
#Instance method execution
product1.create
#output
The product name is iphone
The price is 100000
python
class Product
attr_accessor :name
attr_accessor :price
#10% OFF method for orders of 10 or more
def get_total_price(count)
total_price = self.price * count
if count >=10
total_price *= 0.9
end
return total_price
end
end
#Instance generation
product1 = Product.new
#Set a value in an instance variable
product1.name = "iphone"
product1.price = 100000
#Instance method execution
puts product1.get_total_price(10)
#output
900000
A function that is automatically executed when an instance is created. (Constructor such as python)
Pass an argument when creating an instance and set an instance variable.
python
class class name
def initialize(Argument name)
processing
end
end
#Instance generation
Instance name=name of the class.new(argument)
Example ①
class Product
attr_accessor :name
attr_accessor :price
def initialize(name,price)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
#Instance generation
product1 = Product.new("iphone", 100000)
puts product1.info
#output
Product name: iphone, price: 100000
Example ② (keyword argument)
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
#Instance generation
product1 = Product.new(name:"iphone", price:100000)
puts product1.info
#output
Product name: iphone, price: 100000
require" ./ filename "
`
└ No extension ".rb" required
▼ Read "product.rb" that describes the class definition at the beginning of "index.rb".
product.rb
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
index.rb
#File reading
require "./product"
#Instantiate with the class of the read file
product1 = Product.new(name:"iphone", price:100000)
puts product1.info
#output
Product name: iphone, price: 100000
--File division of class definition and instantiation. --Create multiple instances and store them in an array. --Call each instance one by one with each method. --Execute the info method on each instance.
product.rb
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
index.rb
#File reading
require "./product"
#Instantiate with the class of the read file
product1 = Product.new(name:"iphone6s", price:60000)
product2 = Product.new(name:"iphone8", price:80000)
product3 = Product.new(name:"iphoneX", price:100000)
product4 = Product.new(name:"iphone11pro", price:120000)
#Store instances in an array
products = [product1,product2, product3, product4]
#Extract instances one by one with each method
products.each do |product|
#Execute info method on each instance
puts product.info
#output
Product name: iphone6s, price: 60000
Product name: iphone8, price: 80000
Product name: iphoneX, price: 100000
Product name: iphone11Pro, Price: 120,000
--Combine each method with a variable that increments by 1. --Output index number and info method by variable expansion.
index.rb
#File reading
require "./product"
#Instantiate with the class of the read file
product1 = Product.new(name:"iphone6s", price:60000)
product2 = Product.new(name:"iphone8", price:80000)
product3 = Product.new(name:"iphoneX", price:100000)
product4 = Product.new(name:"iphone11pro", price:120000)
#Store instances in an array
products = [product1,product2, product3, product4]
#Define variables for index numbers
index = 0
#Extract instances one by one with each method
products.each do |product|
#Execute info method on each instance
puts "#{index}. #{product.info}"
index += 1
#output
0.Product name: iphone6s, price: 60000
1.Product name: iphone8, price: 80000
2.Product name: iphoneX, price: 100000
3.Product name: iphone11Pro, Price: 120,000
Variable name = gets.chomp
Stores the contents entered before the enter key is pressed in a variable. * Stored as a character string
Since the information received by gets.chomp is a character string, it is converted to a numerical value.
Variable name = gets.chomp.to_i
Receive input data
puts "Please enter a nickname"
name = gets.chomp.to_i
puts "Please enter your age"
age = gets.chomp.to_i
future_age = age+30
puts "#{name}30 years later#{future_age}is"
Receive the instance index number and order quantity.
--Select the instance that corresponds to the received input value. --Receive the order quantity and return the total amount.
▼ Class-defined file
product.rb
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
index.rb
#File reading
require "./product"
#Instantiate with the class of the read file
product1 = Product.new(name:"iphone6s", price:60000)
product2 = Product.new(name:"iphone8", price:80000)
product3 = Product.new(name:"iphoneX", price:100000)
product4 = Product.new(name:"iphone11pro", price:120000)
#Store instances in an array
products = [product1,product2, product3, product4]
#Define variables for index numbers
index = 0
#Extract instances one by one with each method
products.each do |product|
#Execute info method on each instance
puts "#{index}. #{product.info}"
index += 1
#Receive the item number
puts "Please enter the item number"
order = gets.chomp.to_i
selected_product = products[order]
#Receive the number of orders
puts "The selected product is#{selected_product.name}is"
puts "How many pieces do you want to order?"
count = gets.chomp.to_i
#Calculation of total amount
total_price = selected_product.price * gets.chomp.to_i * 1.1
puts "The total amount is#{total_price}is"
Inherit the instance variables and instance methods of the parent class.
class child class name <parent class name
└ Capital letters at the beginning
└Finally end required
└ Parent class is on top (or read in another file)
Inheritance
require "./File name of parent class"
class child class name<Parent class name
end
Files are roughly classified into four types. Parentheses are examples of file names.
--File that defines the parent class (parent class name.py) --File that defines the child class (child class name.py) --File for instantiation (data.py) --Output file (index.py)
A file that defines child classes is created for each child class. (Depending on the amount of code, it may be combined into one file)
Simply define a new instance variable in the inherited class.
ʻAttr_accessor: instance variable name `
product.rb
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
▼ Create Phone class by inheritance. Added instance variable weight.
phone.rb
require "./product"
class Phone < Product
attr_accessor :weight
end
▼ Set the value to the instance generated by the child class Phone
index.rb
require "./phone"
#Instance generation
phone1 = Phone.new(name: "iphone11", price: "120000")
#Set the value to the added instance variable
phone1.weight = "194g"
puts phone1.weight
#output
194g
Simply define a new instance method in the inherited class.
▼ Added a method to the above phone.rb.
self. instance name
└ Calling instance variables in a class
phone.rb
require "./product"
class Phone < Product
attr_accessor :weight
def set_property
return "#{self.name}Weighs#{self.weight}is"
end
end
▼ Calling additional methods
index.rb
require "./phone"
#Instance generation
phone1 = Phone.new(name: "iphone11", price: "120000")
#Set the value to the added instance variable
phone1.weight = "194g"
#Call the added method
puts phone1.set_property
#output
iphone 11 weighs 194g
Overwriting instance variables and methods. (If it is already defined in the parent class, it will be overridden)
product.rb
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
▼ Override the info method of the parent class
phone.rb
require "./product"
class Phone < Product
attr_accessor :weight
#override
def info
return "product name:#{self.name},price:#{self.price}Circle, weight:#{self.weight}"
end
end
▼ Calling the overridden method
index.rb
require "./phone"
#Instance generation
phone1 = Phone.new(name: "iphone11", price: "120000")
#Set the value to the added instance variable
phone1.weight = "194g"
#Call the added method
puts phone1.info
#output
Product name: iphone11, price: 120,000 yen, weight: 194g
Call a method that has already been defined in initialize.
super (argument: variable of parent class)
▼ Parent class
product.rb
class Product
attr_accessor :name
attr_accessor :price
#Where to call with override
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
end
phone.rb
require "./product"
class Phone < Product
attr_accessor :weight
#Override of initialize (call existing part)
def initialize(name:, price:, weight:)
super(name: name, price: price)
self.weight = weight
end
#override
def info
return "product name:#{self.name},price:#{self.price}Circle, weight:#{self.weight}"
end
end
require" class name "
└ Different from reading a file
(require "./filename")
One of the classes defined in Ruby by default.
▼ Loading class
require "date"
▼ Date instance generation
Variable = Date.new (yyyy, m, d)
▼ Output yyyy-mm-dd
python
require "date"
yesterday = Date.new(2020, 5, 30)
puts yesterday
#output
2020-05-30
Check if it is the specified day of the week → Return as a boolean value.
Instance name.day of the week?
python
require "date"
yesterday = Date.new(2020, 5, 30)
puts yesterday.monday?
puts yesterday.saturday?
#output
false
True
Date.today
python
require "date"
date1 = Date.today
puts date1
#output
2020-05-31
--Instance method
--Call to instance
--Instance name.method name
--Definition: def method name
--End at the end of the sentence
--Class method
--Call to class
--Class name.Method name
--Definition: def class name.method name
--End at the end of the sentence
** When using class methods ** -Output results that do not depend on the instance.
Example: Find today's date.
Date.today
You can create your own class methods.
def class name.method name
** ▼ Class method to check if today's date is Monday **
--Read Date class --Define class methods
product.rb
require "date"
class Product
attr_accessor :name
attr_accessor :price
def initialize(name:,price:)
self.name = name
self.price = price
end
def info
return "product name:#{self.name},price:#{self.price}"
end
#Define class method
def Product.is_today_off
today = Date.today
if today.monday
"Today is a regular holiday"
else
self.info
end
▼ Use the defined class method
index.rb
require "./product"
product1 = Product.new(name: "iphone11", price: "120000")
#Calling a class method
Product.is_today_off
Recommended Posts