Singleton definition differences

would someone please explain the difference between these definitions

class A
class << self
def hello
puts ‘hello’
end
end
end

class B
def B.hello
puts ‘hello’
end
end

A.hello
B.hello

Nicholas R. wrote:

would someone please explain the difference between these definitions

class A
class << self
def hello
puts ‘hello’
end
end
end

class B
def B.hello
puts ‘hello’
end
end

A.hello
B.hello

According to p. 36, “Programming Ruby(2nd ed.)”, they are different
syntaxes for accomplishing the same thing. If you define class A like
this:

class A
def sayhi
puts “hi”
end
end

a = A.new
a.sayhi

then you have this situation:

a – instance of class A
|
|
|
V
class A
methods:
—sayhi()
|
|
|
V
class Object
methods:
—send(), etc.

If you define class A like you did:

class A
class << self
def hello
puts ‘hello’
end
end
end

then this is what happens:

a – instance of class A
|
|
|
V
class A
methods:

|
|
|
V
class A’ – a superclass ( or “singleton class”) for A only containing
A’s class methods
methods:
—hello()
|
|
|
V
class Object
methods
–send(), etc.

In effect, you inserted a new class between class A and class Object.
The dot notation does the same thing.

7stud wrote:

In effect, you inserted a new class between class A and class Object.
The dot notation does the same thing.

When you call a method with object a, ruby searches class A for the
method. If ruby can’t find the method in class A, ruby moves up the
hierarchy and searches class A’ (which contains the class methods) for
the method. If the method can’t be found in class A’, then ruby
searches Object for the method.

7stud – wrote:

If the method can’t be found in class A’, <then ruby
searches Object for the method.
>

Whoops. That’s not true, but I won’t elaborate.

Not to hijack the thread but does singleton not refer to a pattern
method that instantiates and returns an object reference and for future
calls returns the reference of the object which has been cached in
memory to prevent instantiation of duplicate instances

On Jan 23, 2008 11:30 AM, Keynan P. [email protected]
wrote:

Not to hijack the thread but does singleton not refer to a pattern
method that instantiates and returns an object reference and for future
calls returns the reference of the object which has been cached in
memory to prevent instantiation of duplicate instances

There are singleton methods in Ruby. They have nothing in common with
the Singleton pattern as you described it. You can define singleton
methods for objects, e.g. in irb
irb(main):001:0> foo = “Hello World”
=> “Hello World”
irb(main):002:0> class << foo
irb(main):003:1> def shuffle
irb(main):004:2> split(//).sort { rand }.join ‘’
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> foo.shuffle
=> “d HWloorlle”

As you can see, you can modify the methods of single objects instead
modifying the whole class.

On Jan 23, 2008 4:10 AM, Nicholas R. [email protected] wrote:

class B
def B.hello
puts ‘hello’
end
end

A.hello
B.hello

I think, there’s no difference between them and I think also, that
they are not called Singleton methods, but class methods.

On Jan 23, 2008, at 6:05 AM, Thomas W. wrote:

they are not called Singleton methods, but class methods.

However, they are methods that belong to a single instance of a class
object, thus making them singleton. So, in fact, they are both.