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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:39:37+00:00 2026-06-11T16:39:37+00:00

I have a Ruby object (an ActiveRecord object, specifically) named User . It responds

  • 0

I have a Ruby object (an ActiveRecord object, specifically) named User. It responds to methods like find_by_id, find_by_auth_token, etc. However, these aren’t methods that are defined via def or define_method. Instead, they are dynamic methods that are handled via method_missing.

I’d like to obtain a reference to one of these methods via Object#method, e.g.:

User.method(:find_by_auth_token)

It doesn’t look like this works though. The best solution I’ve come up with is:

proc { |token| User.find_by_auth_token(token) }

Is there any other way around using such a wrapper method as this? Am I really unable to use Object#method for dynamic methods?

  • 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-06-11T16:39:38+00:00Added an answer on June 11, 2026 at 4:39 pm

    The simplest answer is “no”—the only way to guarantee in general that Object#method(:foo) will return an instance of Method is by defining a method named foo on the object.

    The more complected answer is that you can coerce Object#method(:foo) into returning an instance of Method by overriding Object#respond_to_missing? s.t. it returns true when given :foo. For example:

    class User
      def respond_to_missing?(method_name, include_private = false)
        method_name.to_s.start_with?('find_by_')
      end
    end
    
    m = User.new.method(:find_by_hackish_means)
    # => #<Method: User#find_by_hackish_means>
    

    (It’s up to you to ensure that the method is actually defined):

    m.call
    # => NoMethodError: undefined method `find_by_hackish_means' for #<User:0x123>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
I want to be able to have an object extend Enumerable in Ruby to
I have some ruby code like this: my_hash = { key1: value, key2: value
I have a complex table pulled from a multi-ActiveRecord object array. This listing is
I have a quite long method. It copy an ActiveRecord object with all relations,
I'm using Ruby 1.8.7 and Rails 2.3.8 I have two ActiveRecord Models, A and
How can I get the class name from an ActiveRecord object? I have: result
I have something like this: class Person < ActiveRecord::Base has_many :things do def [](kind)
It seems that ActiveRecord and Ruby's Time object doesn't play nice with times that
I have been getting this error in Ruby 1.9, Rails 3.0, ActiveRecord 3.0: incompatible

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.