[Ruby] Getting started with Ruby 2

2 minute read

initialize

When an object is created 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

The method defined after the line written as private is a private method and can be used from inside the object but not from outside.

class Person
 def initialize(money)
  @money = money
 end

 def billionaire?
  money >= 10000000
 end

 private
 def money
  @money
 end
end

Inheritance

Used when you want to change some of the functions that basically exist in the existing class. Overriding the method of the parent class with the written process of the child class is called “override”**.

  • Use super to call the parent class
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

module

The basic unit of Ruby is an object, and there is a class as a design drawing of the object. In addition to this, Ruby has the concept of “module” ** as a collection of blueprints of a certain behavior in one place.

The difference between modules and classes is that modules cannot create objects. A module represents a series of behaviors, which can be included in a class as a whole using “include”. Screen Shot 2020-07-26 at 12.21.19.png

module Talking
 def talk
  "Bow-wow"
 end
module Walking
 def walk
  "Takete"
 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

Catch exceptions

  1. Write the code that may cause an exception in begin
  2. Describe how to respond to the exception that occurred in rescue.
  3. In addition, describe the post-processing that you want to always do in the ensure regardless of whether an exception occurs or not.
begin
 # Code that may raise an exception
rescue
 #Code corresponding to exception
ensure
 # Code you always want to execute whether or not an exception occurs
end

nil guard

Syntax for entering 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 is assigned and returned when this method is called.

Safe navigation operator

If you call the method using &., no error occurs even if the receiver is nil.

When using #if
name = if object
 object.name
else
 nil
end

When using the #bot operator
name = object&.name

#%notation When all the elements of the array are strings, you can describe the array using “%w”

array1 = ['apple','banana','orange']
#==>["apple", "banana", "orange"]
#↓ same
array1 = %w(apple banana orange)
#==>["apple", "banana", "orange"]

An array 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)

Tags: ,

Updated: