Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@alindeman /there_i_fixed_it.rb secret
Last active Dec 18, 2015

# This is a joke! You'd be crazy to use it.
# But, isn't it interesting?
# Symbol#to_proc normally returns something like proc { |o| o.send(self) }
# This code hacks it such that if a method exists in the *caller's* binding,
# that method is invoked instead.
# Ruby 2.0 only (for debug_inspector)
require 'debug_inspector'
module SymbolExtensions
def to_proc
method = nil do |i|
method = eval "method(:#{self}) rescue nil", i.frame_binding(2)
method ? method.to_proc : super
Symbol.send :prepend, SymbolExtensions
["Hello World", "Ouch", "Fizzbuzz"].each(&:puts) # Method#to_proc
p ["1", "2", "3"].map(&:to_i) # Symbol#to_proc

Haha. Evil.

I always got tired of writing this, no longer :)

["Hello World", "Ouch", "Fizzbuzz"].each(&method(:puts))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.