Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7861037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:32:19+00:00 2026-06-02T22:32:19+00:00

I am currently practicing/learning singleton methods and singleton classes in Ruby, and I have

  • 0

I am currently practicing/learning singleton methods and singleton classes in Ruby, and I have come across a doubt. Given:

myobject = Object.new

Is there any difference between doing this:

def myobject.mymethod
end

and doing this:

class << myobject
 def mymethod
 end
end

If there is, when would we use one or the other? What are the implications?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:32:20+00:00Added an answer on June 2, 2026 at 10:32 pm

    Use ruby –dump ins to research the result, as follows:

    1.

    # test.rb
    myobject = Object.new
    
    def myobject.mymethod
    end
    

    executes ruby –dump ins test.rb

    the output is:

    D:\>ruby --dump ins test.rb
    == disasm: <RubyVM::InstructionSequence:<main>@test.rb>=================
    local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
    [ 2] myobject
    0000 trace            1                                               (   1)
    0002 getinlinecache   9, <ic:0>
    0005 getconstant      :Object
    0007 setinlinecache   <ic:0>
    0009 send             :new, 0, nil, 0, <ic:1>
    0015 setdynamic       myobject, 0
    0018 trace            1                                               (   3)
    0020 putspecialobject 1
    0022 getdynamic       myobject, 0
    0025 putobject        :mymethod
    0027 putiseq          mymethod
    0029 send             :"core#define_singleton_method", 3, nil, 0, <ic:2>
    0035 leave
    == disasm: <RubyVM::InstructionSequence:mymethod@test.rb>===============
    0000 trace            8                                               (   3)
    0002 putnil
    0003 trace            16                                              (   4)
    0005 leave
    

    2.

    # test.rb
    myobject = Object.new
    
    class << myobject
     def mymethod
     end
    end
    

    executes ruby –dump ins test.rb

    the output is:

    D:\>ruby --dump ins test.rb
    == disasm: <RubyVM::InstructionSequence:<main>@test.rb>=================
    local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
    [ 2] myobject
    0000 trace            1                                               (   1)
    0002 getinlinecache   9, <ic:0>
    0005 getconstant      :Object
    0007 setinlinecache   <ic:0>
    0009 send             :new, 0, nil, 0, <ic:1>
    0015 setdynamic       myobject, 0
    0018 trace            1                                               (   3)
    0020 getdynamic       myobject, 0
    0023 putnil
    0024 defineclass      :singletonclass, singletonclass, 1
    0028 leave
    == disasm: <RubyVM::InstructionSequence:singletonclass@test.rb>=========
    0000 trace            2                                               (   3)
    0002 trace            1                                               (   4)
    0004 putspecialobject 1
    0006 putspecialobject 2
    0008 putobject        :mymethod
    0010 putiseq          mymethod
    0012 send             :"core#define_method", 3, nil, 0, <ic:0>
    0018 trace            4                                               (   6)
    0020 leave                                                            (   4)
    == disasm: <RubyVM::InstructionSequence:mymethod@test.rb>===============
    0000 trace            8                                               (   4)
    0002 putnil
    0003 trace            16                                              (   5)
    0005 leave
    

    So def myobject.method the way is that first sends core#define_singleton_method message to the myobject ,
    then, the core#define_singleton_method message’s implementation gets the myobject’s singleton class, and add method definition into
    the singleton class.

    class << myobject the way is that first sends singletonclass message to the myobject , then, send core#define_method message to the myobject’s singletonclass.

    In practice,

    if you want to define a some of singleton methods for the obj, use class << obj.

    if you only define a singleton method for the obj, use def obj.method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have been practicing coding problems online. Currently I am working on a
I am currently working on Project Euler for fun, and use Haskell for practicing.
Currently I have a class that looks like this: public class MyClass : IMyClass
Currently I have a DetailsView attached to a GridView. When you select an item
Currently, I have: <html> <div class=yes1><span><img src=img></span></div> </html> <script> var x = ='yes1' var
Currently I am adding object by creating it like: type TRecord = class private
currently I'm developing an app for WP7 but came across a little problem with
currently we have a column with only integer values declared as NUMBER. At the
I'm wondering what's a good online judge for just practicing algorithms. I'm currently not
Currently I have the following Mapping in my Controller: @RequestMapping( value = /add.html, method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.