[For programming beginners] What is a method?

Introduction

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 ...

I don't understand what I don't understand

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.

Main subject

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!

Method interpretation

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 calledclass methods`. This is the only difference.

Write your own method

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.

Finally

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 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.

Recommended Posts

[For programming beginners] What is a method?
What is object-oriented programming? ~ Beginners ~
What is a snippet in programming?
Recommended learning method for programming beginners
What is programming?
[For super super beginners] What is object orientation?
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]
What is a constructor?
What is a stream
What is a Servlet?
What is a wrapper class?
What is a boolean type?
What is a Ruby module?
What is a floating point?
What is the pluck method?
What is a meaningful comment?
What is a jar file?
What is a Java collection?
What is a lambda expression?
[Ruby] What is `!!` used for?
What is the constructor for?
What is a fa⁉ enum?
What is the initialize method?
Finally, create a method for whether there is any character
What is a column Boolean type?
What is a reference type variable?
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
What kind of method is define_method?
What is a Ruby 2D array?
[For beginners] You will definitely understand in 10 minutes! What is JavaBeans?
[For beginners] Ruby is said to be ruby, but what about it?
What is a class in Java language (3 /?)
What is a terminal? -Absolute path & relative path-
What is a Spring Boot .original file?
A memorandum for android application development beginners
Learn Java with "So What" [For beginners]
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Made a one-liner method for Premium Friday
What is Docker? What purpose is it used for?
What is the main method in Java?
What is the Facade pattern useful for?
[Rails] What is a dot (.) Or a colon (:)?
What is a request scope? Image commentary
A simple and convenient method for HashMap
Major remodeling! Practical programming learning method for beginners to learn through reform!
What an inexperienced person is thinking now after a month of self-taught programming for the first time.
What is Cubby
What is Docker?
What is null? ]
A brief summary of Bootstrap features for beginners
What is java
Introduction to Recursive Functions: What is a Recursive Function?
What is Keycloak
What is maven?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?