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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:13:37+00:00 2026-05-25T02:13:37+00:00

In ruby, I want to create a method on a class which will save

  • 0

In ruby, I want to create a method on a class which will save a callable object of any sort into an instance variable. This includes lambdas, blocks, and Procs. For example:

obj.save_callable(lambda { |x| x * 2 })
assert_equal(10, obj.invoke_callable(5))

obj.save_callable { |x| x * 3 }
assert_equal(15, obj.invoke_callable(5)) 

obj.save_callable(Proc.new { |x| x * 4 })
assert_equal(20, obj.invoke_callable(5)) 

I know this can be a hairy area. One approach that I’ve already seen is to create different methods, one for each type:

class MyClass
  # pass proc or lambda
  def save_callable(p)
    @callable = p
  end

  # pass a block
  def save_callable_block(&b)
    @callable = b
  end

  def invoke_callable(*args)
    @callable && @callable.call(*args)
  end
end

Question:
Is there some way to boil this down further to just a single save_callable method?

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

    Alright… after writing this question, on a lark, I tried the following. This actually appears to work in both ruby 1.8.7 and 1.9.2:

    class UnifiedSaveCallable
      def save_callable(p=nil, &b)
        @callable = p || b
      end
      def invoke_callable(*args)
        @callable && @callable.call(*args)
      end
    end
    
    obj = UnifiedSaveCallable.new
    
    obj.save_callable(lambda { |x| x * 2 })
    assert_equal(10, obj.invoke_callable(5))
    
    obj.save_callable { |x| x * 3 }
    assert_equal(15, obj.invoke_callable(5)) 
    
    obj.save_callable(Proc.new { |x| x * 4 })
    assert_equal(20, obj.invoke_callable(5)) 
    

    This idiom seems to work for me. Still interested in hearing if there’s a better or more idiomatic way to do this in ruby.

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

Sidebar

Related Questions

I want to create a alias method in ruby and pass parameters to this.
I'm generally studying Ruby at the time. I want to create a variable length
I am using Ruby on Rails. I want to create a filter field on
I want to run a standalone ruby script in which I need my RoR
I want to fill a map with class name and method, a unique identifier
How can I mock the initialize method on a ruby class? I'm doing some
I just want to create a regular expression out of any possible string. var
Hi I'm new in ruby and rails and I want to create a new
Is it possible to add a core method to the Module class in Ruby?
I want to create a very simple window manager for Ubuntu using Ruby. Where

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.