Skip to content

Instantly share code, notes, and snippets.

@benolee
Created August 25, 2012 02:47
Show Gist options
  • Select an option

  • Save benolee/3459320 to your computer and use it in GitHub Desktop.

Select an option

Save benolee/3459320 to your computer and use it in GitHub Desktop.
a naive implementation of dataflow variables in ruby
# Usage: run tests with `ruby dataflow.rb`
require 'bundler/setup'
module DataFlow
class BasicObject < ::BasicObject
undef_method :==
undef_method :equal?
def raise(*args)
::Object.send(:raise, *args)
end
end
class DataFlowVar < BasicObject
def initialize
@mutex = ::Mutex.new
@locked = false
end
def value=(value)
if @locked
raise ::StandardError, "value already assigned"
else
@mutex.synchronize do
@locked = true
@value = value
end
end
end
def value
@value
end
end
end
if __FILE__ == $0
require 'minitest/autorun'
class DataFlowVarTest < MiniTest::Unit::TestCase
def setup
@var = DataFlow::DataFlowVar.new
end
def test_assignment
@var.value = "foo"
@var.value.must_equal "foo"
end
def test_double_assignment_raises
@var.value = "foo"
-> { @var.value = "bar" }.must_raise StandardError
end
end
end
source :rubygems
gem 'minitest'
@tmiller
Copy link

tmiller commented Aug 25, 2012

You should also move require 'bundler/setup' into the test condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment