Introduction to Ruby 2

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

Inheritance

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

module

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. Screen Shot 2020-07-26 at 12.21.19.png

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

Exception catching

  1. Write the code in begin that may cause an exception
  2. Describe how to handle the exception that occurred in it in rescue
  3. In addition, describe the post-processing that you want to do regardless of whether an exception occurs or not in ensure.
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

nil guard

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.

Bocchi operator (safe navigation operator)

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

%notation

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

Introduction to Ruby 2
[Ruby] Introduction to Ruby Error statement
Ruby Learning # 1 Introduction
Introduction to SWING
Introduction to Ruby processing system self-made
Introduction to web3j
Introduction to Micronaut 1 ~ Introduction ~
Introduction to Ruby (from other languages)
[Java] Introduction to Java
Introduction to migration
Introduction to java
Introduction to Doma
Introduction to Ruby basic grammar with yakiniku
Introduction to JAR files
Introduction to Ratpack (8)-Session
Introduction to RSpec 1. Test, RSpec
Introduction to bit operation
Introduction to Ratpack (6) --Promise
Introduction to Ratpack (9) --Thymeleaf
Introduction to PlayFramework 2.7 ① Overview
Introduction to Android Layout
Introduction to design patterns (introduction)
Introduction to Practical Programming
Introduction to javadoc command
From Java to Ruby !!
Introduction to jar command
Introduction to Ratpack (2)-Architecture
Introduction to lambda expression
Introduction to java command
Introduction to RSpec 2. RSpec setup
Introduction to Keycloak development
Introduction to javac command
Introduction to Design Patterns (Builder)
[Introduction] Try to create a Ruby on Rails application
Introduction to RSpec 5. Controller specs
Introduction to RSpec 6. System specifications
Introduction to Android application development
Introduction to RSpec 3. Model specs
Introduction to Ratpack (5) --Json & Registry
How to use Ruby return
Convert to Ruby Leet string
Introduction to Metabase ~ Environment Construction ~
Introduction to Ratpack (7) --Guice & Spring
(Dot installation) Introduction to Java8_Impression
Introduction to Design Patterns (Composite)
Introduction to Micronaut 2 ~ Unit test ~
[Ruby] How to comment out
Introduction to JUnit (study memo)
Introduction to Spring Boot ① ~ DI ~
Introduction to design patterns (Flyweight)
[Java] Introduction to lambda expressions
Introduction to Spring Boot ② ~ AOP ~
Introduction to Apache Beam (2) ~ ParDo ~
Introduction to EHRbase 2-REST API
Rbenv command to use Ruby
Ruby: How to use cookies
Introduction to design patterns Prototype
GitHub Actions Introduction to self-made actions
[Ruby] How to write blocks
[Java] Introduction to Stream API
Introduction to Design Patterns (Iterator)