Chart type ruby-VI (hello class)
A template for creating objects (data that is a variable used in a program, methods that are behaviors are bundled into one unit).
Wikipedia: Class and wikipedia: Object-Oriented Programming
class Greeter
def initialize
@name = gets_name
puts_hello
end
def puts_hello
puts "Hello #{@name}."
end
def gets_name
name = ARGV[0] || 'world'
return name
end
end
Greeter.new
The function in class becomes method (method: behavior of object)
The following four keys are the keys this time
-Greeter.new: Call method outside of class
-Class Greeter: Class declaration
-Initialize method: Initializes the data in the class
-@Name: Instance variable initialize method
#Inherited class
class Greeter < String
def hello
"Hello #{self}."
end
end
#Inheriting class
class String
def hello
"Hello #{self}."
end
end
A class can pass data and methods that it has in another class (inheritance)
In the above sample program, Greeter is passed from String
Q: What is double quotation?
A: "← This guy
In emacs, when processing a character string, enclose it in double quotation marks like "character string".
In emacs, there is a function to determine where the character string is by color. Even if you make a mistake and do not enclose it in double quotation marks, if you look at the color, you will notice the mistake in one shot.
Recommended Posts