Those who have recently started programming, especially those who are enthusiastic about "I'll do it on my own!" Without going to school!
Yes. I was the same. And it's the same where I'm working on Ruby for no reason or thought. Friend!
But ...
That's right. Write the code! I don't know what to write! Well I think that's normal.
Occasionally, there are mutants (geniuses) who open their eyes and write even metapros in about half a year, but it is better not to worry about it. Most of these people are kind and good at teaching, so let's teach them variously.
Not limited to Ruby, but where programming beginners often stumble or have difficulty understanding
Method
, argument
Isn't it? I was.
Write your own method
I didn't understand the meaning of this. Oh nostalgic and frustrating.
It took me some time to find a way of thinking that fits my needs. (I got into practice very quickly, so I got a feel for it. I don't recommend studying for a long time.)
There are also instance methods
and class methods
in the methods, and I have no idea how or where to use them! It was like that.
So I would like to give you my own interpretation here!
Let's open a terminal and start ʻirb`. Try typing the following:
irb
hoge = 'hoge is hoge hoge'
Then execute the following code.
irb
hoge.length
You are a genius with a result of 9. Great.
Yes. The length
at this time is the method.
e? Do you know that? As expected. You are no longer a beginner.
Next is here
irb
class User
def name
'hoge'
end
end
Then run the following code.
irb
user = User.new
user.name
You are a genius with the result " hoge "
. Great.
Yes. The name
at this time is the method.
all right! You are already taking the next step. You won't have to keep reading this article.
Hmm? If you think so, please read it a little more.
What I just did
Length method
of String class
ʻThe name method
of the User class `
There are two.
Please refer to the following URL for the length method
.
String # length (Ruby 2.7.0 Reference Manual)
The length method
of the String class
is pre-built into Ruby. Returns the character length of the object.
The name method
of the ʻUser class` is the one I created this time. The contents can be hoge or fuga.
Both of these are instance methods
. Use it by sticking it to an instance of each class.
You can create an instance of the Hoge class
with Hoge.new
.
Methods that are used by directly attaching to ʻUser or
Hogeare called
class methods`. This is the only difference.
At first I misunderstood that this was very difficult.
But if you understand the concept, you can use it very conveniently. This time I wrote name
and the content was only hoge, but by writing various processes together, you can execute it by hitting that method with one shot.
For example (let's use arguments this time)
irb
class Number
def check(a, b)
if a == 2
a * b
elsif a == b
a / b
else
a + b
end
end
end
Now let's execute the check method
of the Number class
.
irb
num = Number.new
num.check(1, 2)
num.check(2, 2)
num.check(3, 3)
It's a method whose calculation formula changes depending on the argument. I think it's a good idea to write and execute various simple ones using ʻirb`. Did you feel comfortable with the arguments now?
The class method
is the best of all, and is very useful when doing batch processing. Just a blow! It's like that.
What is batch processing? If you think, here
In short, it is a mechanism to execute the accumulated processing behind the system.
I don't know after reading this far! Those who say. I'm really sorry. That's the confusingness of this article. I will study so that I can write more clearly.
By the way, even if you don't know much about the method at this moment, it's okay if you can use ʻirbor the
length` that came out earlier on the console. I will understand it soon. When you find out or become familiar with it, kindly teach beginners at that time.
Thank you for reading until the end.
Recommended Posts