I thought I'd read the Cherry book and output it, but when it comes to creating my own program, it's difficult to solve. .. ..
I have collected ** basic problems ** for those who have the same problems as me.
Even if it is a basic problem, honestly, if you solve this problem with ambiguous knowledge, you may not understand it.
If possible, I think it is better to proceed without looking at the answer, but if you do not understand by all means, I will write the answer at the bottom of the article so please refer to it.
And since I myself said that I couldn't understand even after reading the answer, I will describe the points that I did not understand.
sample.rb
class Car
def self.run
@@count += 1
end
def count
@@count
end
end
car1 = Car.new
car1.run
car2 = Car.new
car2.run
car1.run
puts Car.count
I referred to Problem of this article.
●
(Expected output)
-Calling the instance method run
increments the class shared counter by 1.
-Calling the class method `count`
returns the current counter value.
If 3 is displayed, it's okay.
●
I have quoted this article.
food.rb
class Food
def eat
puts "I like."
end
end
natto = Food.new()
wasabi = Food.new()
karaage = Food.new()
natto.eat #=>I like.
wasabi.eat #=>I don't like.
karaage.eat #=>I love.
●
I referred to this article.
●
sample.rb
class Music
def mc
puts "This is #{@genre} of #{self.class.to_s}"
end
def initialize(genre)
@genre = genre
end
end
Rap.new("mc-battle").mc
(Output example)
This is mc-battle of Rap
Yo, mic check 1, 2.
You can't solve it unless you understand how to use class variables and class methods.
●
--Classes and their instances are scoped --Similar to constants, but different in that class variables can be changed as many times as you like --Accessible within class methods, instance methods, and class definition expressions
The following articles were helpful and very helpful in solving this problem. https://qiita.com/mogulla3/items/cd4d6e188c34c6819709
First, the points to look at are ** 2, 6 lines **.
sample.rb
class Car
def self.run #2nd line
@@count += 1
end
def count #6th line
@@count
end
end
car1 = Car.new
car1.run
car2 = Car.new
car2.run
car1.run
puts Car.count
Didn't you notice?
Look at the last line.
puts Car.count
This seems to be overhanging the Car class count method, isn't it strange?
When calling the class as it is, the class method must be issued.
If you fix it
#### **`sample.rb`**
```rb
class Car
def self.run #2nd line
@@count += 1
end
def self.count #6th line
@@count
end
end
#-Omission-
puts Car.count #Last line
And the second line is also strange.
This must be an instance method.
sample.rb
class Car
def run #2nd line/def self.Change from run
@@count += 1
end
end
car1 = Car.new
car1.run
Because, in the class, an instance is created with `` `car1```, so it must be an instance method as a method to call that instance.
But that's not the end.
** The class variable @@ count
is not defined. ** **
sample.rb
class Car
@@count = 0 #Definition of class variables
def run #Class method
@@count += 1 #@@count is a class variable
end
def self.count #Instance method
@@count
end
end
car1 = Car.new
car1.run
car2 = Car.new
car2.run
car1.run
puts Car.count
# => $ ruby sample.rb
# => 3
This is okay.
What is a singular method in the first place? ** Refers to a method specific to one instance. ** **
Singular methods can be defined with ``` def object name. Method name` ``. https://qiita.com/k-penguin-sato/items/d637dced7af32e4ec7c0
Therefore,
food.rb
class Food
def eat
puts "I like."
end
end
natto = Food.new()
wasabi = Food.new()
karaage = Food.new()
#--add to--↓
def wasabi.eat
puts "I don't like."
end
def karaage.eat
puts "I love."
end
#--add to--↑
natto.eat
wasabi.eat
karaage.eat
Define methods with the same name but different behaviors for objects that are instances of the same class.
This is a singular method, which is not for the class, but for the instance made from the class.
Therefore, you can output from each method.
(result)
I like.
I don't like.
I love.
sample.rb
class Music
def mc
puts "This is #{@genre} of #{self.class.to_s}"
end
def initialize(genre)
@genre = genre
end
end
Rap.new("mc-battle").mc
- Self.class.to_s
: Converts the class name of the instance itself created like `` `Rap.new``` to a character string.
Now, let's create `Rap class`
.
sample.rb
#--Omission--
class Rap < Music
end
Look at the output characters.
Yo, mic check 1, 2.
Since it is an output that is not defined in the Music class, we will output it in the Rap class.
If you look at ``` Rap.new ("mc-battle "). Mc``` on the last line, the method uses ``` mc```, so I will describe it.
#### **`sample.rb`**
```rb
#--Omission--
class Rap < Music
def mc
end
end
Since it inherits the Music class
, if you describe the necessary part
sample.rb
#--Omission--
class Rap < Music
def mc
puts "Yo, mic check 1, 2."
end
end
But this is not the end.
We haven't been able to call the Music class yet.
What should i do?
** Execute the "super" method. ** **
The super method * finds and executes a method in the superclass that has the same method name as the called method. ** **
Then, if you rewrite
sample.rb
class Music
def mc
puts "This is #{@genre} of #{self.class.to_s}"
end
def initialize(genre)
@genre = genre
end
end
class Rap < Music
def mc
super #Additional code
puts "Yo, mic check 1, 2."
end
end
Rap.new("mc-battle").mc
done.
Recommended Posts