Ruby Review 2
About this post h1>
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 h1>
○ Override h3>
-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 h3>
・ 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 h3>
-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 h3>
-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 h3>
・ 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 h3>
・ What can be described directly with ruby
・ Ex) Numbers, letters, arrays, hashes, etc.
○ Encapsulation h3>
-Protective action that prohibits access to methods from outside the class
・ One of the object-oriented concepts
② Module h1>
-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 h1>
-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.