Skip to content

Instantly share code, notes, and snippets.

@h3h h3h/thing.rb
Created Apr 12, 2010

Embed
What would you like to do?
Sharing a value slot across multiple objects.
class Thing
@@things = {}
def initialize
@pointer = self.hash
end
def set(x)
@@things[@pointer] = x
end
def get
@@things[@pointer]
end
def merge(thing2)
set(thing2.get)
thing2.pointer = @pointer
end
def pointer=(p)
@pointer = p
end
end
require 'thing'
describe Thing do
before(:all) do
@t1 = Thing.new
@o1 = Object.new
@t2 = Thing.new
@o2 = Object.new
@t1.set(@o1)
@t2.set(@o2)
@t3 = Thing.new
@t3.set(Object.new)
@t4 = Thing.new
@t4.set(Object.new)
end
it "should return the same thing from get that was passed to set" do
@t1.get.should == @o1
@t2.get.should == @o2
end
it "should merge values from one thing to another" do
@t1.merge(@t2)
@t1.get.should == @t2.get
@t1.get.should == @o2
end
it "should merge values across three things" do
@t2.merge(@t3)
@t2.get.should == @t3.get
@t1.get.should == @t2.get
@t1.get.should == @t3.get
end
it "should merge values across four things" do
@t1.merge(@t4)
@t1.get.should == @t2.get
@t2.get.should == @t3.get
@t3.get.should == @t4.get
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.