Fun with Superators
Jay Phillips wrote a wicked little gem called Superators this summer, and I’ve been looking for an excuse to use it. Here’s a superator to change the protection of a method:
require 'rubygems'
require 'superators'
class Class
superator "<-" do |method_id|
private method_id
end
superator "<+" do |method_id|
public method_id
end
superator "<~" do |method_id|
protected method_id
end
end
This allows you to quickly access private methods:
puts [].rand rescue "rand is private!"
Array <+ "rand"
puts [].rand
Dangerous, but useful if you need to keep a method private but you want to test it as if it was public (think: testing your rails models).
describe Foo, "#bar" do
before(:each) do
# Foo#bar is private
Foo <+ "bar"
end
after(:each) do
Foo <- "bar"
end
it "should do something amazing!" do
bar.should be_amazing
end
end
Enjoy.
0 comments