Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton method vs. class method

Are class method and singleton method doing the same or different? Here is an example.

class C
  def self.classmethod
    puts "class method #{self}"
  end
end

C.classmethod  # class method C
c = C.new

def c.singletonmethod
  puts "instance method #{self}"
end

c.singletonmethod  # instance method #<C:0x0000000118ed08>
like image 219
rubyemerite Avatar asked Dec 23 '15 06:12

rubyemerite


People also ask

What is the difference between normal class and singleton class?

A singleton class can only have one instance whereas a normal class can have many. Ie. A normal class allows you do this: NormalClass object1 = new NormalClass() NormalClass object2 = new NormalClass() For a singleton class you must do SingletonClass.

Which is better singleton or static class?

A Singleton class can Dispose, while a static class can not. A Singleton class can have a constructor, while a static class can only have a private static parameterless constructor and cannot have instance constructors. A Static class has better performance since static methods are bonded on compile time.

What is the difference between singleton and static method and when to use?

The difference between the Singleton and Static is Singleton Class can have value when Class object instantiated between server and client, such a way if three client want to have a shared data between them Singleton can be used. Static are always just shared and have no instance but multiple references.

Can singleton class have methods?

In case of singleton classes there is no use of static methods as there is only one instance available of the class and every buddy is having the same copy of it.


1 Answers

Most of what happens in Ruby involves classes and modules, containing definitions of instance methods

class C
  def talk
    puts "Hi!"
  end
end

c = C.new
c.talk
Output: Hi!

But as you saw earlier (even earlier than you saw instance methods inside classes), you can also define singleton methods directly on individual objects:

obj = Object.new
def obj.talk
  puts "Hi!"
end
obj.talk
#Output: Hi!

When you define a singleton method on a given object, only that object can call that method. As you’ve seen, the most common type of singleton method is the class method—a method added to a Class object on an individual basis:

class Car
  def self.makes
    %w{ Honda Ford Toyota Chevrolet Volvo }
  end
end

But any object can have singleton methods added to it. The ability to define method- driven behavior on a per-object basis is one of the hallmarks of Ruby’s design.

Singleton classes

Singleton classes are anonymous: although they’re class objects (instances of the class Class ), they spring up automatically without being given a name. Nonetheless, you can open the class-definition body of a singleton class and add instance methods, class methods, and constants to it, as you would with a regular class.

Note:

Every object has two classes:

■ The class of which it’s an instance

■ Its singleton class

----------------------------------------------------------------

At Last I would highly recommends you to watch.

1: The Ruby Object Model and Metaprogramming For detail info about singleton method vs. class method ruby

2: MetaProgramming - Extending Ruby for Fun and Profit - by Dave Thomas

Hope this help you!!!

like image 188
Gupta Avatar answered Sep 29 '22 12:09

Gupta