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!
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
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:
hoge = 'hoge is hoge hoge'
Then execute the following code.
You are a genius with a result of 9. Great.
length at this time is the method.
e? Do you know that? As expected. You are no longer a beginner.
Next is here
class User def name 'hoge' end end
Then run the following code.
user = User.new user.name
You are a genius with the result
" hoge ". Great.
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
name method of the User class `
There are two.
Please refer to the following URL for the
String # length (Ruby 2.7.0 Reference Manual)
length method of the
String class is pre-built into Ruby. Returns the character length of the object.
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
Methods that are used by directly attaching to ʻUser
are calledclass 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)
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
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?
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 ʻirb
or thelength` 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.