Home>

I'm new to programming.
RecentlyThis pageI am practicing by watching.
When I tackled some problems and saw my answer, I had one question.
That's how it's implemented.

Most problems are implemented as class methods, but thisThe problem of finding the total value for each row and columnIs implemented in a module.

What are the advantages of implementing a simple exercise in a class method (maybe you can define the method in the top class), and the advantage of defining it in a module instead of a class method?

(Addition) 1, calendar creation problem

Not the answer associated with this questionThis pageThere was an answer of the author. This was an instance method.

2, Karaoke machine problem

Answer templateSo it was an instance method.

3, Bingo card creation problem

There is no CodelQ page leftThis pageYou can see from the answer template that the class method is specified.

4, Bonus drink problem

Answer templateSo it was a class method.

5, Phonebook creation problem

Answer templateSo it was a class method.

6, National Holidays.csv Perth Program

This pageSo it was a class method.

7, "In Ruby, count English words contained in English articles and sort them in order of appearance" problem

This pageThen the calling part is a class method and the processing part is an instance method.

8, A program that calculates the total value in units of rows and columns

This pageThen it was a module.

9, Feature phone character input problem

There was no answer from the author.

10, Price tag split problem

Writer's answerThen it was a method.

result

50% are class methods, 20% are instance methods, 10% are modules, 20% are others
have become.

  • Answer # 1

    "Most problems are implemented as class methods"
    Is this true?
    Isn't it "define a class and its instance method"?
    If it is implemented as a class method, there is almost no point in creating a class, and I think it would be better if the method definition is at the top level (less complicated).

    I don't think there is much to say which is better or worse depending on the way of thinking about class or module.
    Class is "solve by yourself" method is problem-specific and does not consider other diversion
    Modules "leave it to others" method is versatile and can be used for other themes
    Is it like that?
    However, the quoted "How to solve with a module" does not seem to be a reference. Unnecessarily complicated.
    This problem is a one-line bastard without creating a class.

    Postscript
    I don't think it's a theme to read and consider all the examples, so I happened to open the "phone book creation problem".
    Including this, such sites will be evaluated automatically. If you do not specify anything, the name will be different and it will not be possible to apply automatic evaluation, so we will regulate how to call the program. In "Phonebook Creation Problem"NameIndex.create_index (names)I have declared that I will call it.
    so,
    As long as it is made in class, the entrance must be a class method. I'm not saying that you should write all the implementations in class methods, and if you do, you will be deducted as an object-oriented program.
    I often write this way too.
    class CsvIo def self.create (data) new (data) .make end def make Messed up end end
    It feels like. Create an instance with the class method. The instance does the actual work.

    The meaning of "method is versatile and can be used for other themes" can be understood by looking at how to use the built-in module Math method.

  • Answer # 2

    Advantages of implementing simple exercises with class methods (maybe you can define methods with top class)

    I think the biggest reason is that it is inappropriate because it has limited use to make it a top-level instance method. It's also a by-product, but is it a descriptive name for the defined methods?

    Also, what are the benefits of defining it in a module rather than a class method?

    It's not like Ruby because the methods are used functionally. I don't think there is any particular advantage.
    Define a classgenerate_matrixI improved the processing ofinitialize initializeI think that it will be like Ruby if you do it.


    The purpose of the linked article seems to be "improvement of Ruby power", so I may have dared to write it in a way that I do not usually do. Even if that is not the case, if it was an opportunity to feel a question and investigate like the questioner this time, I think that is very meaningful.