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 7008985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:48:19+00:00 2026-05-27T21:48:19+00:00

Inspired by this article , I was playing around with the Ruby method calling

  • 0

Inspired by this article, I was playing around with the Ruby method calling hierarchy and noticed something strange.

Given:

class B
  def foo  
    "- Instance method defined by B"
  end  

  def method_missing(method)
    puts "- method_missing (#{method}) on b. Redirecting to b.foo\n"
    foo
  end
end

b = B.new

def b.foo  
  "- Method defined directly on an instance of B\n" + super  
end

def b.method_missing(method)
  "- method_missing (#{method}) on b. Calling super\n" + super
end

puts "Calling 'bar' on b of type #{b.class}:"
puts b.bar

Running it gives:

Calling 'bar' on b of type B:
- method_missing (bar) on b. Redeirecting to b.foo
- method_missing (bar) on b. Calling super
- Method defined directly on an instance of B
- instance method defined by B

My question is:

Since I am calling b.bar (on the object), how come the class’s instance method is called before the objet’s instance method is called?

I would have expected b.method_missing(method) to be called first, then the class’s instance method_missing(method) (since I am calling super? but super is class hierarchy…) which the does the redirect from bar to foo.
Also, how come, after being redirected to foo, the instance’s missing_method is called? We were just told that we’re being redirected…

I think I don’t understand the concept of how Ruby allows to define an instance method on an instance of a class (new to me), as opposed to defining it as an instance method of a the class (classic languages).

I hope this question makes sense, maybe my head is still spinning from last night…

  • 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-27T21:48:19+00:00Added an answer on May 27, 2026 at 9:48 pm

    The problem is that your output does not reflect the actual order of execution. Let’s take a look at the output:

    method_missing (bar) on b. Redeirecting to b.foo
    method_missing (bar) on b. Calling super
    

    At first glance, this gives the impression that B#method_missing is called before the singleton method b.method_missing which raises the two questions you describe. Actually though, b.method_missing is correctly called first. This gets clear if you look at how the statement puts b.bar is evaluated:

    1. b has no method bar, so b.method_missing is called with the string 'bar' as an argument.
    2. b.method_missing in turn calls the method B#method_missing through use of the super keyword.
    3. B#method_missing uses puts to output the first line seen above
    4. the singleton method b.foo is called
    5. the outermost puts is performed, which outputs the string in b.method_missing along with the return value of b.foo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is partially inspired by this article written by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx Using
Inspired by this CodingHorror article, Protecting Your Cookies: HttpOnly How do you set this
Today I encountered this article about decimal expansion and I was instantaneously inspired to
This question is inspired by the article Why are Facebook, Digg, and Twitter so
Inspired by this question. Why is there no list.clear() method in python? I've found
Inspired by this answer .. Can you guys point me to something similar--something that
Inspired by this question ( Find an integer not among four billion given ones
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question and answer , how do I create a generic permutations
Inspired by this question I began wondering why the following examples are all illegal

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.