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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:57:35+00:00 2026-05-13T08:57:35+00:00

I’m trying to create a little Ruby hack to make something like the reverse

  • 0

I’m trying to create a little Ruby hack to make something like the reverse of the Symbol#to_proc hack. Whereas the Symbol#to_proc hack makes this possible:

some_array.each(&:some_method)

is the same as

some_array.each { |obj| obj.some_method }

I want to make this possible:

some_array.each(&[:some_method])

would be the same as

some_array.each { |obj| some_method(obj) }

The problem is that unless some_method is a kernel method, what it would really mean is:

some_array.each { |obj| self.some_method(obj) }

When an block is passed explicitly, it’s created within the binding where this line is written, so self correctly accesses the current object and everything works fine. However if the block is created in the Array’s to_proc method, self is within the Array binding (and refers to the array itself). Perhaps it’s not possible to do what I’m trying to do, but I’d love some ideas from the community.

Here’s my hack so far. This works as long as the method is a kernel method, and is available within Array:

class Array
  def to_proc
    lambda {|i| send(self.first, i)}
  end
end

With this hack, this line works, and puts the three symbols:

[:foo, :bar, :bas].each(&[:puts])

It’s useful to me already, because often I’m finding myself wanting to inspect an array of ActiveRecord objects within the console and it saves me from typing { |i| puts i}. But it would be even more useful to be able to use it within objects so that some method within the object gets called with every item in the array passed as a parameter.

So any idea how to make the binding issue work?

Please don’t both responding with “you don’t want to do this because it’ll make things run slower” or “you’re not saving so many characters typing, but it makes the code less readable” — I know these things already. 🙂 This is more of a feeding my curiosity kind of hack. Just wondering if it’s possible.

  • 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-13T08:57:36+00:00Added an answer on May 13, 2026 at 8:57 am

    You are looking for the method called method 🙂

    class Example
      def some(arg)
        p arg.size # just as a demo
      end
    end
    
    obj = Example.new
    %w{the quick brown fox jumps over the lazy dog}.each(&obj.method(:some))
    

    yields

    3
    5
    5
    3
    5
    4
    3
    4
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 473k
  • Answers 473k
  • 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 see this post: Google Maps & apps with mapview have… May 16, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer Please read about the Linux Filesystem, i-nodes and d-nodes. http://en.wikipedia.org/wiki/Inode_pointer_structure… May 16, 2026 at 4:04 am
  • Editorial Team
    Editorial Team added an answer You may want to explore double buffering - if your… May 16, 2026 at 4:04 am

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.