initialize When creating an object with the new method, the initialize method of that object is executed
class User
attr_reader :name, :address, :email
def initialize(name, address, email)
@name = name
@address = address
@email = email
end
end
private Methods defined after the line that says private are private methods and can be used from inside the object but not from the outside.
class Person
def initialize(money)
@money = money
end
def billionaire?
money >= 10000000
end
private
def money
@money
end
end
Used when you want to change some of the functions of an existing class after basically inheriting all the functions Overriding the processing of the method of the parent class with the processing written by the child class is called ** "override" **.
class PricedObject #Parent class, super class
def total_price
price * Tax.rate
end
def price
raise NotImplementedError
end
end
class product < PricedObject #Child class, subclass
attr_accessor :price
end
class OrderedItem < PricedObject #Child class, subclass
attr_accessor :unit_price, :volume
def price
unit_price * volume
end
end
The basic unit of Ruby is an object, and there is a class as a blueprint for an object. In addition, Ruby has the concept of ** "module" ** as a collection of blueprints for a series of behaviors in one place.
The difference between a module and a class is that a module cannot create an object. A module represents a set of behaviors that you can use ** "include" ** to get them all into a class.
module Talking
def talk
"Bow-wow"
end
module Walking
def walk
"Teke Teke"
end
end
class Dog
include Talking
include Walking
end
mugi = Dog.new
mugi.talk
mugi.walk
#How to write using inheritance
class PricedObject
def total_price
price * Tax.rate
end
def price
raise NotImplementedError
end
end
class product < PricedObject
attr_accessor :price
end
class OrderedItem < PricedObject
attr_accessor :unit_price, :volume
def price
unit_price * volume
end
end
#How to write using modules
module PricedHolder
def total_price
price * Tax.rate
end
end
class product
include PriceHolder
attr_accessor :price
end
class OrderedItem
include PriceHolder
attr_accessor :unit_price, :volume
def price
unit_price * volume
end
end
begin
#Code that may raise an exception
rescue
#Code corresponding to the exception
ensure
#Code that you always want to execute with or without an exception
end
Syntax to enter a value if the variable is nil
#Example 1
a = nil
a ||= 3
puts a
# ==> 3
#Example 2
def people
@people ||= []
end
# ==>@Even if people is nil, an empty array will be assigned and returned when this method is called.
Calling a method using &. Does not cause an error even if the receiver is nil.
#When using if
name = if object
object.name
else
nil
end
#When using the Bocchi operator
name = object&.name
If all the elements of the array are strings, you can use ** "% w" ** to describe the array
array1 = ['apple', 'banana', 'orange']
#==>["apple", "banana", "orange"]
#↓ Same
array1 = %w(apple banana orange)
#==>["apple", "banana", "orange"]
Arrays in which all elements are symbols can be described using ** "% i" **
array1 = [:apple, :banana, :orange]
#==>[:apple, :banana, :orange]
#↓ Same
array1 = %i(apple banana orange)
#==>[:apple, :banana, :orange]
[Reference | Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E4%BD % BF% E3% 81% 88% E3% 82% 8B-Ruby-Rails-5% E9% 80% 9F% E7% BF% 92% E5% AE% 9F% E8% B7% B5% E3% 82% AC % E3% 82% A4% E3% 83% 89-% E5% A4% A7% E5% A0% B4% E5% AF% A7% E5% AD% 90 / dp / 4839962227)
Recommended Posts