Ruby Review 2

About this post This is a continuation of the previous [Ruby Review 1](https://qiita.com/naoto911/items/f46b35f84cc80f59cac3). Please refer to [Ruby Review 1](https://qiita.com/naoto911/items/f46b35f84cc80f59cac3) for "Purpose" and "Materials".

① Note on terms

○ Override -Method overwriting when inheriting from a parent class to a child class -Child classes take precedence even for methods with the same name

○RoboCop

・ "Are you observing Ruby rules?" Software that automatically determines ・ Because there are so many rules and regulations, the purpose is to set the sun if it is processed manually.

○ Object ・ Generic term for numerical values, processing, etc. ・ All one set in ruby is called an object ・ The hierarchy advances in the order of class → method → value

○ Destructive editing -Saving the result of value rewriting etc. -Add "!" To the end of the code ・ If this is not done, the stored value will return to the original value.

○ expression expansion -Type of writing method when a variable is included in the output of a character string

qiita.rb


name = 'naoto'
puts "my name is #{name}"

・ # {Name} is the expression expansion ・ The important thing is to enclose it in double coating ("") </ font>. ・ When this is executed, the following is displayed.

qiita.rb


my name is naoto

○ Dynamic typing ・ Notation that does not require type specification -In normal programming, the type is described and the value is stored when the variable is declared. ・ Ruby can store values suddenly

○ Literal ・ What can be described directly with ruby ・ Ex) Numbers, letters, arrays, hashes, etc.

○ Encapsulation -Protective action that prohibits access to methods from outside the class ・ One of the object-oriented concepts

② Module -Constants should define a class as one set ・ If you compare it with Duema, constants correspond to cards, classes correspond to packs, and modules correspond to boxes.

-Inheritance between modules is not possible -Cannot create an instance ⇨ Self is required because instance methods cannot be used (self. By describing the process as a method name Instance name.Method name ⇨ Module name.Method name and image to be rewritten)

qiita.rb


module Driver
 def self.run
   puts 'Run'
 end
 def self.stop
    puts 'Stop'
 end 
end

Driver.run
Driver.stop

③ Exception handling -Interrupt the process by describing the process that is likely to cause an error and its response. -Write "processing after interruption" and "message" in begin ~ end

qiita.rb


puts '--- Please enter an integar. ---'
i = gets.to_i
    
begin 
    puts 10/i
rescue  => ex
    puts 'Error!'
    puts ex.message
    puts ex.class
ensure
    puts 'end'
    
end

[Explanation] ・ Begin ~ end ・ Flow proceeds to the last error occurrence point in this ・ Rescue => ex ⇨ Describe the processing when an error occurs here ・ Ensure ⇨ Describe the last process to be executed regardless of whether an exception has occurred.


Recommended Posts

Ruby Review 2
Ruby Review 1
Ruby cherry book review
Review of Ruby basic grammar
Ruby learning 4
Rails Review 1
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Extraction of "ruby" double hash * Review
Ruby addition
Refactoring Ruby
Ruby learning 3
Java review
Encapsulation review
Ruby setting 2
Ruby problem ⑦
Basics of Ruby ~ Review of confusing parts ~
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
[Ruby] Review about nesting of each
Ruby settings 1
Refactoring Ruby
Ruby Basics 2 ~ Review of confusing parts ~
Ruby basics
Ruby memo
Ruby learning 1
[Ruby] Module
Rails Review 2
With ruby ● × Game and Othello (basic review)
Introduction to Ruby 2
Review about bundler
ruby Uppercase letters
ruby search problem
Ruby Learning # 25 Comments
Ruby to_s method
[Ruby] postfix if
[Ruby] FizzBuzz problem
Ruby text conversion
Ruby basic terms
ruby exception handling
Ruby Learning # 13 Arrays
About Ruby symbols
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
[ruby] drill output
I started Ruby
[Ruby] Iterative processing
ruby API problem
Ruby vertical writing
About ruby ​​form
[ruby] drill output
About Ruby Hashes
Ruby setting 3 Rubocop
Ruby Learning # 14 Hashes
[ruby] drill output
[Ruby] each nested