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 Randal 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:
-- <no 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.

···

--
Posted via http://www.ruby-forum.com/\.

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 4:10 AM, Nicholas Randal <nrandal@dslextreme.com> 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

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.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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

they are not called Singleton methods, but class methods.

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.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/.

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 11:30 AM, Keynan Pratt <keynan@howe.textdrive.com> 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