Skip to content

Instantly share code, notes, and snippets.

@h3h
Created April 12, 2010 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h3h/363718 to your computer and use it in GitHub Desktop.
Save h3h/363718 to your computer and use it in GitHub Desktop.
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