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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:28:04+00:00 2026-05-27T13:28:04+00:00

For (auto-)educational purposes, I’m trying to mimic the super behavior to learn how it

  • 0

For (auto-)educational purposes, I’m trying to mimic the super behavior to learn how it works.

I could mimic super for instance methods, but I couldn’t do it for class methods.

Here is my code:

class A
  def aa
    @msg ||= 'Original...: '
    puts "#{@msg}#{self}.aa: #{self.class} < #{self.class.superclass}"
  end
  def self.ab
    @msg ||= 'Original...: '
    puts "#{@msg}#{self}.ab: #{self} < #{self.superclass}"
  end
end

class B < A
  def aa
    @msg = "Real super.: "
    super
  end
  def self.ab
    @msg = "Real super.: "
    super
  end
  def mimic_aa
    @msg = "Mimic super: "
    self.class.superclass.instance_method(:aa).bind(self).call
  end
  def self.mimic_ab
    @msg = "Mimic super: "
    #superclass.method(:ab).unbind.bind(self).call
      #=> Error: singleton method only works in original object

    #superclass.ab
      #=> self is A; I want self to be B

    proc = superclass.method(:ab).to_proc

    #self.instance_eval(&proc)
      #=> ArgumentError: instance_eval seems to call aa(some_unwanted_param)
      # Note: Ruby 1.8.7

    #eval('proc.call', binding)
      #=> self is A; I want self to be B

  end
end

a = A.new
b = B.new

a.aa         #=> Original...: #<A:0xb77c66ec>.aa: A < Object
b.aa         #=> Real super.: #<B:0xb77c6624>.aa: B < A
b.mimic_aa   #=> Mimic super: #<B:0xb77c6624>.aa: B < A

puts ''

A.ab         #=> Original...: A.ab: A < Object
B.ab         #=> Real super.: B.ab: B < A
B.mimic_ab   #=> (expected the same as above)

Any ideas?

  • 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-27T13:28:05+00:00Added an answer on May 27, 2026 at 1:28 pm

    Well, the problem is probably the version of Ruby that you are using. I’m running 1.9.2, and the program runs as expected. I think (from the comment in your code) that the problem is that you are running Ruby v1.8.7. It also wouldn’t hurt to try running your code again.

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

Sidebar

Related Questions

We use Auto Scaling and it works pretty good for us, but this morning
I'm trying to auto-generate a plain text email with a trademark symbol in it.
Auto formatting ( gg=G ) works perfectly for code like so (example from here
It auto detect my current location when i run this app but there isn't
I am trying to auto increment my xml file. I have a form that
My auto builds are running fine, but after some versioning customization, the name of
Personally I hate auto-created desktop shortcut icons, but some folks seem to think that
My app must NOT auto -rotate at all. But it includes a screen which
Trying to auto-wire properties to a bean in Spring 3.0.5.RELEASE , I'm using: config.properties
I am trying to auto fill city and state value after user typing 5-digit

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.