Singleton Methods

Could anyone explain what exactly we meant by a Singleton Method ?
See if i am thinking it right....

class A
end
a = A.new
a.instance_eval <<-EOS
  def wishing_hello
    puts "HELLO !!"
  end
EOS

so, here wishing_hello will be a singleton method ?

···

--
sur

Hi --

Could anyone explain what exactly we meant by a Singleton Method ?
See if i am thinking it right....

class A
end
a = A.new
a.instance_eval <<-EOS
def wishing_hello
  puts "HELLO !!"
end
EOS

so, here wishing_hello will be a singleton method ?

Yes. A singleton method is basically a method that belongs to one
object only. Since methods actually "belong to" classes and modules,
rather than objects in general, Ruby implements singleton methods by
means of singleton classes. Each object has its own, dedicated class
-- its singleton class -- where its singleton methods are stored.

You can gain direct access to an object's singleton class like this:

   class << a # from your example
     def wishing_goodbye
       puts "GOODBYE!!"
     end
   end

wishing_goodbye is now defined as an instance method in the singleton
class of a.

David

···

On Sat, 11 Nov 2006, sur max wrote:

--
                   David A. Black | dblack@rubypal.com
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

:fr sur max [mailto:sur.max@gmail.com]
# Could anyone explain what exactly we meant by a Singleton Method ?
# See if i am thinking it right....

fr ola bini's blog http://ola-bini.blogspot.com/2006/09/ruby-singleton-class.html.

just in case you want to think ruby :slight_smile:

kind regards -botp

Thanks David !!

···

On 11/11/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Sat, 11 Nov 2006, sur max wrote:

> Could anyone explain what exactly we meant by a Singleton Method ?
> See if i am thinking it right....
>
> class A
> end
> a = A.new
> a.instance_eval <<-EOS
> def wishing_hello
> puts "HELLO !!"
> end
> EOS
>
> so, here wishing_hello will be a singleton method ?

Yes. A singleton method is basically a method that belongs to one
object only. Since methods actually "belong to" classes and modules,
rather than objects in general, Ruby implements singleton methods by
means of singleton classes. Each object has its own, dedicated class
-- its singleton class -- where its singleton methods are stored.

You can gain direct access to an object's singleton class like this:

   class << a # from your example
     def wishing_goodbye
       puts "GOODBYE!!"
     end
   end

wishing_goodbye is now defined as an instance method in the singleton
class of a.

David

--
                   David A. Black | dblack@rubypal.com
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

--
sur