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

  • Home
  • SEARCH
  • 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 7688613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:57:42+00:00 2026-05-31T19:57:42+00:00

I’m reading Metaprogramming Ruby and just want to clear something up about the following

  • 0

I’m reading Metaprogramming Ruby and just want to clear something up about the following paraphrased code:

class MyClazz
  def self.my_class_method(name)
    define_method(name) {
      # do stuff
    }
  end

  my_class_method :foo
  my_class_method :bar
end

# The code above generates instance methods:

# def foo
#   do stuff
# end

# def bar
#   do stuff
# end

Q1
My first question regards the two method calls at the end of the file: my_class_method :foo and my_class_method :bar. Am I right in thinking that they are both invoked automatically when a MyClazz object is instantiated?

Q2 When Ruby generates these methods (def foo and def bar), it will put them in MyClazz’s eigenclass, even though they are instance methods. So does this mean that Ruby looks to the eigenclass for both class and instance methods when needed?

I just want to clear that up before I move too far into the book.

  • 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-05-31T19:57:43+00:00Added an answer on May 31, 2026 at 7:57 pm

    Answer 1: (short) They are invoked when ruby instantiate MyClass instance (of type Class).

    (long) When Ruby interpreter sees a class definition (class MyClazz) it instantiate an instance of this class and evaluate all the code inside class definition.

    In your case MyClazz is a constant that holds a refference to the object of class Class. And when Ruby initialize it, it execute code inside class definition – defines singleton method my_class_method of this Class instance and executes method my_class_method twice in the context of this Class instance.

    Answer 2: (short) Module#define_method private method adds method to the method table of Class instance (method table holds instance methods of the class). It does not affect eigenclass of istance object/class object.

    (long) When you call instance method on the object, Ruby first looks this method in the eigenclass of this object, then in the superclass of eigenclass (it will be a Class object of the object’s class). But it will not look in the eigenclass of MyClazz
    object.

    Example:

    obj = MyClazz.new
    obj.foo # => ok
    

    obj.foo will look for foo method definition in eigenclass of obj object, then will look for instance methods of MyClass (instance of class Class), then in the superclass of MyClass object (in your case this is Object class) etc.

    obj = MyClass.new
    MyClass.my_class_method :baz
    obj.baz # => ok
    

    MyClass.my_class_method will look for my_class_method method definition in the eigenclass of MyClass object (sidenote: eigenclass of a class sometimes is
    called a metaclass) and it will find it here and will add baz instance method to the class MyClass.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.