Last active
August 29, 2015 14:01
-
-
Save ejfinneran/90694503baea1d5c1cb3 to your computer and use it in GitHub Desktop.
This file contains 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
gem 'minitest' |
This file contains 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 Object | |
alias_method :unsafe_send, :send | |
alias_method :safe_send, :public_send | |
alias_method :send, :safe_send | |
end | |
This file contains 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
require './safe_send' | |
require 'minitest/autorun' | |
class MyClass | |
def my_public_method | |
"Hello" | |
end | |
private | |
def my_private_method | |
"Get out of here!" | |
end | |
end | |
describe MyClass do | |
it "should allow me to call #my_public_method" do | |
MyClass.new.my_public_method.must_equal "Hello" | |
end | |
it "should not allow me to call #my_private_method" do | |
MyClass.new.my_public_method.must_equal "Hello" | |
end | |
it "should not allow me to call #my_private_method" do | |
assert_raises(NoMethodError) { | |
MyClass.new.my_private_method | |
} | |
end | |
it "should not allow me to call #my_private_method using send" do | |
assert_raises(NoMethodError) { | |
MyClass.new.send(:my_private_method) | |
} | |
end | |
end |
/Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest.rb:21:in `public_send': private method `attr_accessor' called for #<Class:Minitest> (NoMethodError)
from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest.rb:21:in `<module:Minitest>'
from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest.rb:9:in `<top (required)>'
from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest/autorun.rb:8:in `require'
from /Users/ej/.rvm/gems/ruby-2.1.1/gems/minitest-5.3.3/lib/minitest/autorun.rb:8:in `<top (required)>'
from safe_send_test.rb:2:in `require'
from safe_send_test.rb:2:in `<main>'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Got this far and then minitest stopped working because it expected
send
to work with private methods. ಠ_ಠ