Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Methods should be private

For the same reasons `puts` is private: it makes no use of `self` and it
doesn't make sense to call with an explicit reciever (e.g. `self.puts` or
`Object.puts` wouldn't make sense).
  • Loading branch information...
commit c6a39087a154885a20392b05dc1ee406c72efca9 1 parent 9776611
@misfo misfo authored
Showing with 8 additions and 3 deletions.
  1. +1 −0  lib/rns.rb
  2. +7 −3 spec/rns/rns_spec.rb
View
1  lib/rns.rb
@@ -57,6 +57,7 @@ def add_methods(to, use_spec)
to.send(:define_method, name) do |*args|
from.method(name).call(*args)
end
+ to.send(:private, name)
end
end
end
View
10 spec/rns/rns_spec.rb
@@ -24,8 +24,8 @@ class Thing
extend Rns.module_with(Math::Arithmetic => [:inc])
include Rns.module_with(Math::Statistics => [:avg])
- def inced_one
- self.class.inc 1
+ def self.inced_one
+ inc 1
end
def average
@@ -69,7 +69,11 @@ def average
end
it "works with private module methods" do
- Thing.new.inced_one.should == 2
+ Thing.inced_one.should == 2
+ end
+
+ it "should add methods privately" do
+ lambda { Thing.new.avg [11, 42, 7] }.should raise_error(NoMethodError)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.