Skip to content

Instantly share code, notes, and snippets.

@arwagner
Created July 14, 2011 20:58
Show Gist options
  • Save arwagner/1083428 to your computer and use it in GitHub Desktop.
Save arwagner/1083428 to your computer and use it in GitHub Desktop.
Shadowing in ruby
class User
def self.ceo
new("Fred")
end
def self.all_managers
[User.new("Wilma")]
end
def initialize name
@name = name
end
def manager
manager = User.ceo
User.all_managers.each {|manager| return manager if self.reports_to?(manager) }
return manager
end
def reports_to? manager
false
end
end
describe User do
describe "#manager" do
context "when there are no managers" do
it "should return the CEO" do
ceo = stub User
User.stub :ceo => ceo, :all_managers => []
User.new("Andrew").manager.should == ceo
end
end
context "when there is a manager to whom the user reports" do
it "should return that manager" do
ceo = stub User
wilma = stub User
User.stub :ceo => ceo, :all_managers => [wilma]
user = User.new("Andrew")
user.stub :reports_to => true
user.manager.should == wilma
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment