Permalink
Browse files

Refactoring: more generic semantics + don't pollute global namespace.

  • Loading branch information...
1 parent fc84a3f commit 47198c1f81631c61bb301903825dae817bf3c8f1 @mynyml committed Apr 1, 2009
Showing with 12 additions and 10 deletions.
  1. +11 −9 lib/every.rb
  2. +1 −1 test/test_every.rb
View
@@ -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
View
@@ -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])

0 comments on commit 47198c1

Please sign in to comment.