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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:49:12+00:00 2026-05-11T02:49:12+00:00

Is it possible in Ruby to get a reference to methods of an object

  • 0

Is it possible in Ruby to get a reference to methods of an object ( I would like to know if this can be done without procs/lambdas ) , for example , consider the following code :

 class X   def initialize     @map = {}     setup_map   end    private   def setup_map     # @map['a'] = get reference to a method     # @map['b'] = get reference to b method     # @map['c'] = get referebce to c method   end    public   def call(a)     @map['a'](a) if a > 10     @map['b'](a) if a > 20     @map['c'](a) if a > 30   end    def a(arg)      puts 'a was called with #{arg}'   end    def b(arg)      puts 'b was called with #{arg}'   end    def c(arg)     puts 'c was called with #{arg}'   end end 

Is it possible to do such thing ? I would like to avoid procs/lambdas because I want to be able to change the behaviour of A,B,C by subclassing .

  • 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. 2026-05-11T02:49:13+00:00Added an answer on May 11, 2026 at 2:49 am

    You want Object#method:

    ---------------------------------------------------------- Object#method      obj.method(sym)    => method ------------------------------------------------------------------------      Looks up the named method as a receiver in obj, returning a Method       object (or raising NameError). The Method object acts as a closure       in obj's object instance, so instance variables and the value of       self remain available.          class Demo           def initialize(n)             @iv = n           end           def hello()             'Hello, @iv = #{@iv}'           end         end          k = Demo.new(99)         m = k.method(:hello)         m.call   #=> 'Hello, @iv = 99'          l = Demo.new('Fred')         m = l.method('hello')         m.call   #=> 'Hello, @iv = Fred' 

    Now your code becomes:

    private def setup_map   @map = {     'a' => method(:a),     'b' => method(:b),     'c' => method(:c)   }   # or, more succinctly   # @map = Hash.new { |_map,name| _map[name] = method(name.to_sym) } end  public def call(arg)   @map['a'][arg] if arg > 10   @map['b'][arg] if arg > 20   @map['c'][arg] if arg > 30 end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only way to achieve that with a query would… May 12, 2026 at 11:05 am
  • Editorial Team
    Editorial Team added an answer setjmp()/longjmp() completely subvert stack unwinding and therefore exception handling as… May 12, 2026 at 11:05 am
  • Editorial Team
    Editorial Team added an answer This error "Microsoft.XLANGs.Core.ServiceCreationException : Failed while creating a XXX service"… May 12, 2026 at 11:05 am

Related Questions

An idiom commonly used in OO languages like Python and Ruby is instantiating an
In a Ruby web application I want users to be able to upload documents.
I have a PHP CLI script, that processes a csv file, inserting it's content
I have read a lot that LISP can redefine syntax on the fly, presumably

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.