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
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.
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.
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 class
generate_matrixI improved the processing of
initialize 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.
- ruby - i'm using the errors method, but i don't get an error message
- ruby - improvement of nomethoderror undefined method `paginate'
- ruby on rails - how to test that a specific element has a class name using rspec/capybara
- [java] about equals method (reason why nullpointerexception does not occur even if null is passed/override)
- ruby - [bootstrap] even if card-img-top is added to class, the image is not placed at the top
- ruby - undefined method `save'for nil: nilclass cannot be resolved
- ruby on rails: undefined method ʻuser'for nil: about nomethoderror of nilclass
- ruby - no method error rails
- ruby on rails - [rails] i want to make it a helper method
- java - i don't understand the method call inside the class type method
- ruby - binary data conversion method
- ruby - i want to define a human class
- ruby on rails - [rails] i'm implementing a search function using form_with, but i'm having trouble with the search results not b
- ruby - the method defined in the model cannot be used
- ruby on rails - error when implementing rails6 follow function
- [ruby] "attr_accessor" i don't understand this method
- ruby - nomethoderror: undefined method `new'for: uglifier: symbol error on heroku deployment
- ruby on rails 5 - undefined method `tomorrow'for 00: i would like to know how to solve bigdecimal
- ruby on rails5 nomethoderror (undefined method `cart_items' for nil:nilclass) implementation of ec site cart function
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning