Skip to content

Commit

Permalink
Refactoring: more generic semantics + don't pollute global namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
mynyml committed Apr 7, 2009
1 parent fc84a3f commit 47198c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lib/every.rb
@@ -1,17 +1,19 @@
class Every
instance_methods.each { |m| undef_method(m) unless m.match(/^__/) }
def initialize(enum)
@enum = enum
end
def method_missing(method, *args, &block)
@enum.map {|o| o.__send__(method, *args, &block) }
module Enumerable
class Proxy
instance_methods.each { |m| undef_method(m) unless m.match(/^__/) }
def initialize(enum, method=:map)
@enum, @method = enum, method
end
def method_missing(method, *args, &block)
@enum.__send__(@method) {|o| o.__send__(method, *args, &block) }
end
end
end

module Enumerable
def every(&block)
block_given? ?
Every.new(self).instance_eval(&block) :
Every.new(self)
Proxy.new(self).instance_eval(&block) :
Proxy.new(self)
end
end
2 changes: 1 addition & 1 deletion test/test_every.rb
Expand Up @@ -8,7 +8,7 @@ class EveryTest < Test::Unit::TestCase
context "Every" do
test "is a basic object" do
whitelist = %w( __id__ __send__ method_missing )
Every.instance_methods.to_set.should be(whitelist.to_set)
Enumerable::Proxy.instance_methods.to_set.should be(whitelist.to_set)
end
test "passes message onto enumerable's items" do
[1.4, 2.4, 3.4].every.floor.should be([1,2,3])
Expand Down

0 comments on commit 47198c1

Please sign in to comment.