Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Learning ruby and oop in general, I came upon class methods which as far as I understand are like instance methods but are accessed from the class rather than from an object and can have only one running at the same time.

However, I don't understand why you would use a class method versuses a normal method (outside a class) and what is even their use?

For example:

#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

As they both produce the same result? I'm quite confused about them so please correct if I'm misunderstanding any/everything here.

like image 390
user2521439 Avatar asked Aug 27 '13 17:08

user2521439


People also ask

What is the purpose of static methods?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

Are class methods static methods Ruby?

Its true that class methods in ruby are the closest thing to static methods, but there's more to them than just that. Since class methods are actually defined on an object, it is important to think of them as instance method (belonging a metaclass) - because thats what they are.

How do you make a method static in Ruby?

To declare a static variable, we just need to declare a class variable that will act as a static one, as it will be common to all the instances of the class. In simple terms, when we talk about Ruby, the static variables are declared using the class variable.

How do you use the class method in Ruby?

Class Methods are the methods that are defined inside the class, public class methods can be accessed with the help of objects. The method is marked as private by default, when a method is defined outside of the class definition. By default, methods are marked as public which is defined in the class definition.


1 Answers

Your example isn't a good one.

Class methods might deal with managing all instances that exist of a class, and instance methods deal with a single 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 named Book. It has a class method all_by_author. It queries some pretend database and returns an array of Book instances. The instance method title fetches the title of a single Book instance.

So the class method managing a collection of instances, and the instance method manages just that instance.


In general, if a method would operate on a group of instances, or is code related to that class but does not directly read or update a single instance, then it probably should be a class method.

like image 186
Alex Wayne Avatar answered Sep 28 '22 06:09

Alex Wayne