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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:28:47+00:00 2026-05-23T03:28:47+00:00

I have a couple of modules that extend method missing: module SaysHello def respond_to?(method)

  • 0

I have a couple of modules that extend method missing:

module SaysHello
    def respond_to?(method)
        super.respond_to?(method) || !!(method.to_s =~ /^hello/)
    end
    def method_missing(method, *args, &block)
        if (method.to_s =~ /^hello/)
            puts "Hello, #{method}"
        else
            super.method_missing(method, *args, &block)
        end
    end
end

module SaysGoodbye
    def respond_to?(method)
        super.respond_to?(method) || !!(method.to_s =~ /^goodbye/)
    end
    def method_missing(method, *args, &block)
        if (method.to_s =~ /^goodbye/)
            puts "Goodbye, #{method}"
        else
            super.method_missing(method, *args, &block)
        end
    end
end

class ObjectA
    include SaysHello
end

class ObjectB
    include SaysGoodbye
end

This all works well, eg ObjectA.new.hello_there outputs "Hello, hello_there". Likewise, ObjectB.new.goodbye_xxx outputs "Goodbye, xxx". respond_to? also works, eg ObjectA.new.respond_to? :hello_there return true.

However, this doesn’t work very well when you want to use both SaysHello and SaysGoodbye:

class ObjectC
    include SaysHello
    include SaysGoodbye
end

While ObjectC.new.goodbye_aaa works correctly, ObjectC.new.hello_a acts strange:

> ObjectC.new.hello_aaa
Hello, hello_aaa
NoMethodError: private method `method_missing' called for nil:NilClass
    from test.rb:22:in `method_missing' (line 22 was the super.method_missing line in the SaysGoodbye module)

It outputs correctly, then throws an error. Also respond_to? doesn’t correctly, ObjectC.new.respond_to? :hello_a returns false.

Finally, adding this class:

class ObjectD
    include SaysHello
    include SaysGoodbye

    def respond_to?(method)
        super.respond_to?(method) || !!(method.to_s =~ /^lol/)
    end


    def method_missing(method, *args, &block)
        if (method.to_s =~ /^lol/)
            puts "Haha, #{method}"
        else
            super.method_missing(method, *args, &block)
        end
    end
end

Also acts strangely. ObjectD.new.lol_zzz works, however ObjectD.new.hello_aand ObjectD.new.goodbye_t both throw a name exception after outputting the correct string. respond_to? also fails for hello and goodbye methods.

Is there a way to get this all working correctly? An explanation of how method_missing, Modules and super are interacting would also be really useful.

EDIT: coreyward solved the problem, if I use super instead of super.<method-name>(args...) in all the methods I define, the program works correctly. I don’t understand why this is though, so I asked another question about this at What does super.<method-name> do in ruby?

  • 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-23T03:28:48+00:00Added an answer on May 23, 2026 at 3:28 am

    When you redefine a method, you redefine a method; period.

    What you’re doing when you include the second module with the method_missing method define is overriding the previously defined method_missing. You can keep it around by aliasing it before you redefine it, but you might want to watch out with that.

    Also, I don’t know why you’re calling super.method_missing. Once your method_missing definition is out of tricks you should let Ruby know it can continue on up the chain looking for a way to handle the call, all just by calling super (no need to pass arguments or specify a method name).

    About Super (update)

    When you call super Ruby continues on up the inheritance chain looking for the next definition of the method invoked, and if it finds one it calls it and returns the response. When you call super.method_missing you call the method_missing method on the response to super().

    Take this (somewhat silly) example:

    class Sauce
      def flavor
        "Teriyaki"
      end
    end
    
    # yes, noodles inherit from sauce:
    #   warmth, texture, flavor, and more! ;)
    class Noodle < Sauce
      def flavor
        sauce_flavor = super
        "Noodles with #{sauce_flavor} sauce"
      end
    end
    
    dinner = Noodle.new
    puts dinner.flavor     #=> "Noodles with Teriyaki sauce"
    

    You can see that super is a method just like any other, it just does some magic behind the scenes. If you call super.class here you’re going to see String, since “Teriyaki” is a string.

    Make sense now?

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

Sidebar

Related Questions

I have a couple different versions of Python installed on my Mac. The default
I have couple of servlets designed to hold its own message source, but there
I have couple of questions: What is exactly VJoyD used for? Based on my
I am looking for information on sending GPS coordinates from a GPS module to
First, please bear with me. I have hard time telling others my problem and
Background: I was dragged into seeing this question: Fibonacci's Closed-form expression in Haskell when
The question is pretty much in the title: in terms of OS-level implementation, how
I am trying to create a gem with a generator for Rails 3 (beta
Technical question: Given a regex: my $regEx = qr{whatever$myVar}oxi; # Notice /o for compile-once
I am preparing for SCJP and I came to know Methods with variable argument

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.