Created
April 14, 2010 06:54
-
-
Save juliocesar/365525 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def self.from_block &block | |
Hash[*BlockBuilder.build(&block)] | |
end | |
class BlockBuilder | |
@@collected = [] | |
class << self | |
def build(&block) | |
instance_eval &block | |
copy = @@collected.clone and @@collected.clear and return copy | |
end | |
def method_missing(method, *args) | |
@@collected.concat [method.to_sym, args.first] | |
end | |
end | |
end | |
end | |
# Hash.from_block do | |
# apple 8 | |
# banana 2 | |
# carrot 6 | |
# end | |
# => {:apple=>8, :banana=>2, :carrot=>6} |
Yep, but all the methods in Class get in the way. puts Class.methods. Hash.from_block { extend 10 } raises an exception for example. But it's not just method calls that mess with the lexical scope of the block, there are also global functions like 'p', 'print', etc. that get in the way too, which is not a problem I can see any way around.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The point of BlockBuilder above is to sandbox the execution of the block away. So that method_missing there won't get in any other class' way.