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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:57:23+00:00 2026-06-13T05:57:23+00:00

I was wondering if or how it is possible to map a function to

  • 0

I was wondering if or how it is possible to map a function to a value of a hash.

For example:
—-Start Class————

def foo(var)
    return var + 2
end

hash_var = { func => foo() }

—-End Class————–

so that I could later call

Class::hash_var["func"][10]

or

Class::hash_var["func"](10)

and that would return 12?

  • 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-13T05:57:24+00:00Added an answer on June 13, 2026 at 5:57 am

    Functions/methods are one of the few things in Ruby that are not objects, so you can’t use them as keys or values in hashes. The closest thing to a function that is an object would be a proc. So you are best off using these…

    The other answers pretty much listed all possible ways of how to put a proc into a hash as value, but I’ll summarize it nonetheless 😉

    hash = {}
    
    hash['variant1'] = Proc.new {|var| var + 2}
    hash['variant2'] = proc     {|var| var + 2}
    hash['variant3'] = lambda   {|var| var + 2}
    
    def func(var)
      var + 2
    end
    
    hash['variant4'] = method(:func) # the *method* method returns a proc
                                     # describing the method's body 
    

    there are also different ways to evaluate procs:

    hash['variant1'].call(2) # => 4
    hash['variant1'][2]      # => 4
    hash['variant1'].(2)     # => 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a complete noob to Automapper and I'm wondering if its possible to map
I'm wondering if its possible to make the following code more concise: $('#americasTrigger').hover( function
I am wondering if it is possible using annotations to persist the attributes map
I am wondering if it is possible to create a route on a map
I am wondering if it is possible to map a named native query on
I'm wondering if it's possible to define a map with unknown (Object) element type.
I am wondering whether or not it is possible to elegantly map the results
I'm wondering if its possible to reference a class definition directly for purpose of
I am wondering if it is possible to map a m:n relationship in the
I was wondering if it is possible to map multiple DTO objects to a

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.