What is the use of class / static methods in ruby?

Studying ruby ​​and oop in general, I came across class methods that, as I understand it, are similar to instance methods, but are accessible from the class, not from the object, and can only have one run at a time.

However, I don’t understand why you are using a class method that compares a regular method (outside the class) and what even uses them?

For instance:

#Why would you use: class Foo def self.bar puts "Class method" end end #Versus simply: def bar puts "Normal method" end Foo.bar # => Class method bar # => Normal method 

How do they both give the same result? I was very confused about them, so please correct if I do not understand anything / everything here.

+6
source share
5 answers

Your example is not very good.

Class methods can deal with the management of all instances that exist in the class, and instance methods process one instance at a time.

 class Book def self.all_by_author(author) # made up database call database.find_all(:books, where: { author: author }).map do |book_data| new book_data # Same as: Book.new(book_data) end end def title @title end end books = Book.all_by_author('Jules Vern') books[0].title #=> 'Journey to the Center of the Earth' 

In this example, we have a class called Book . It has a class method all_by_author . It queries some mock database and returns an array of Book instances. The title instance method selects the title of a single Book instance.

So, the class method that manages the collection of instances, and the instance method manages this particular instance.


In general, if a method works with a group of instances or is code associated with this class, but does not directly read or update a single instance, then it probably should be a class method.

+10
source

As you said, they are:

  • "from the class, not from the object, and
  • "only one start can work at a time.

Also remember that the class is carried over

+3
source

This is a question of OOP than ruby. Class methods in ruby ​​are used in the same way as in other OO programming languages. It means:

  • class methods are executed in the context of the class (and have access only to class variables)
  • instance methods are executed in the context of the object (and have access to the variables of the object or instance).

Here is a better example:

 class Foo def self.bar puts 'class method' end def baz puts 'instance method' end end Foo.bar # => "class method" Foo.baz # => NoMethodError: undefined method 'baz' for Foo:Class Foo.new.baz # => instance method Foo.new.bar # => NoMethodError: undefined method 'bar 

Here you can see that the class method is accessible through the class, and the instance method is accessible through the instance or class object ( Foo.new ).

An example is copied from here , where you can also find additional information on this subject.

Keep in mind: although any code can be placed in a class or instance method, each has its own use cases and its own pros and contrasts. In OOP, we strive for reusable, flexible, and readable code, which means that we usually want to put most of the code structured as instance methods in a reasonable domain model.

+3
source

Most importantly, it keeps your code organized. When you have hundreds of thousands of lines of code, if they all randomly throw things in the same namespace, this can be a nightmare. Organization is really important, and namespaces are an easy way to get modularity with language support.

Somewhat less important, class / module methods can share state without leaking it all over the place (for example, classes can have instance variables), and they can have private support methods to improve factoring, while global methods cannot effectively to be closed.

0
source

You have a lot of misunderstandings,

In ruby, we can define a class and instance method.

The class method is used to provide processing at the class level, that is, data that can only be accessed at the class level or associated with all objects. For example, to count the number of objects that belong to a class, you need a class method. how

 Foo.count 

In the same way, to process a single object, you need an object method to process a single object, for example

 obj.save 

Thus, the class method is an example of a single design pattern, where an object can have its own implementation of the same method.

0
source

Source: https://habr.com/ru/post/952609/


All Articles