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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:14:01+00:00 2026-05-12T12:14:01+00:00

How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,

  • 0

How can I make this code work?

class Meta

    @array = [:a,:b]

    def self.method_missing(name, *args, &block)
            if @array.include? name
                    self.class.send(:define_method, name) do
                            do_call(name)
                    end
            else
                    puts "[#{name.inspect}] is not part of array!"
            end
    end

    def do_call arg
            puts "doing call for ['#{arg}'] "
    end
end

The idea is to have Meta.a (after being defined) to have

def a 
 do_call(a)
end

in its body. But after running this, my output is:

[:do_call] is not part of array!

UPDATE: now kinda working like this:

class Meta

    @array = [:a,:b]

    def self.do_call arg
            puts "doing call for ['#{arg}'] "
    end

    def self.method_missing(name, *args, &block)
            puts "#{name} is missing!"
            if @array.include? name
                    self.class.send(:define_method, name) do
                            do_call name
            end
            else
                    puts "[#{name.inspect}] is not part of array!"
            end
       end
    end

but still, here is an excerpt of an IRB session:

[~/code] $ irb -r meta

irb(main):001:0> Meta.a

a is missing!

=> #

irb(main):002:0> Meta.a

doing call for [‘a’]

=> nil

irb(main):003:0> c = Meta.new

=> #

irb(main):004:0> c.a

NoMethodError: undefined method `a’ for #

    from (irb):4

irb(main):005:0> Meta.methods

=> [“inspect”, “send”, “pretty_inspect”, “class_eval”, “clone”, “yaml_tag_read_class”, > >”public_methods”, “protected_instance_methods”, “send“, “private_method_defined?”,
“equal?”, “freeze”, “do_call”, “yaml_as”, “methods”, “instance_eval”, “to_yaml”, “display”,
“dup”, “object_id”, “include?”, “private_instance_methods”, “instance_variables”, “extend”,
“protected_method_defined?”, “const_defined?”, “to_yaml_style”, “instance_of?”, “eql?”,
“name”, “public_class_method”, “hash”, “id”, “new”, “singleton_methods”,
“yaml_tag_subclasses?”, “pretty_print_cycle”, “taint”, “pretty_print_inspect”, “frozen?”,
“instance_variable_get”, “autoload”, “constants”, “kind_of?”, “to_yaml_properties”, “to_a”,
“ancestors”, “private_class_method”, “const_missing”, “type”, “yaml_tag_class_name”,
“instance_method”, “<“, “protected_methods”, “<=>”, “instance_methods”, “==”,
“method_missing”, “method_defined?”, “superclass”, “>”, “pretty_print”, “===”,
“instance_variable_set”, “const_get”, “is_a?”, “taguri”, “>=”, “respond_to?”, “to_s”, “<=”,
“module_eval”, “class_variables”, “allocate”, “class”, “taguri=”,
“pretty_print_instance_variables”, “tainted?”, “public_instance_methods”, “=~”,
“private_methods”, “public_method_defined?”, “autoload?”, “id“, “nil?”, “untaint”,
“included_modules”, “const_set”, “a”, “method”]

what gives? ‘a’ is a class method and it is not passed on to the new Meta object (c). Why?

  • 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-12T12:14:01+00:00Added an answer on May 12, 2026 at 12:14 pm

    You defined do_call as an instance method while you probably intended to define it as a class method. Which is why it calls method_missing for do_call as well and you get the error you’re getting.

    Also note that when you do self.class.send, self.class will be Class, so the method will be available on all classes, not just meta. You probably rather want:

    class <<self
      self
    end.send
    

    Edit in response to your update:

    ‘a’ is a class method and it is not passed on to the new Meta object (c). Why?

    Because a is a class method[1]. Instances of a class only get the class’s instance methods.

    You’re defining a as an instance method on Class and then you try to call it on an instance of Meta, which does not work. In ruby instance methods of Class as well as singleton methods defined on a class can only be called by doing TheClass.the_method, not instance_of_the_class.the_method. If you want to call a method on instances of Meta, define it as an instance method. If you want to be able to do Meta.a as well as Meta.new.a you have to define both an instance as well as a class method a.

    [1] Actually, as I already said, the way you’re defining it isn’t even a class method of Meta. It’s an instance method of Class (which means you can also call it as e.g. String.a).

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

Sidebar

Ask A Question

Stats

  • Questions 193k
  • Answers 193k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's no built-in way to do this in Hibernate. You… May 12, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer Your code is very verbose - most of the comments… May 12, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer Actually, a machine that you do development on shouldn't be… May 12, 2026 at 6:37 pm

Related Questions

Alright, I'm having a hard time explaining this, let me know if I should
I've got some generic class for my JPA model POJO that goes like this:
I use Simple HTML DOM to scrape a page for the latest news, and
My question involves a few different active records class Respondent < ActiveRecord::Base has_many :phone_numbers,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.