Skip to content

Instantly share code, notes, and snippets.

@tlossen
Created February 21, 2012 15:54
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 tlossen/1877125 to your computer and use it in GitHub Desktop.
Save tlossen/1877125 to your computer and use it in GitHub Desktop.
playing with cloby
require 'rubygems'
require 'cloby'
Ref = Java::clojure.lang.Ref
class Foo < Clojure::Object
attr_accessor :count
def initialize(count = 1)
@count = count
end
def to_s
"[Foo #{count}]"
end
end
class Bar
SIZE = 5
def initialize
@cells = Array.new(SIZE) { Ref.new(rand(10)) }
end
def get(i)
@cells[i].deref
end
def set(i, value)
@cells[i].set(value)
end
def inspect
result = "[Bar "
SIZE.times do |i|
result += "#{get(i)} "
end
result + "]"
end
end
jruby-1.6.6 :001 > require 'example'
=> true
jruby-1.6.6 :002 > foo = dosync { Foo.new }
=> [Foo 1]
jruby-1.6.6 :003 > foo.count
=> 1
jruby-1.6.6 :004 > dosync { foo.count = 3 }
=> 3
jruby-1.6.6 :005 > bar = dosync { Bar.new }
=> [Bar 9 6 3 5 4 ]
jruby-1.6.6 :006 > bar.get(0)
=> 9
jruby-1.6.6 :007 > dosync { bar.set(0, 'x') }
=> "x"
jruby-1.6.6 :008 > bar
=> [Bar x 6 3 5 4 ]
jruby-1.6.6 :009 > dosync { bar.set(2, foo) }
ConcurrencyError: No message available
from org/jruby/clojure/ClojureLibrary.java:73:in `dosync'
from (irb):9:in `evaluate'
from org/jruby/RubyKernel.java:1077:in `eval'
from /Users/tim/.rvm/rubies/jruby-1.6.6/lib/ruby/1.8/irb.rb:158:in `eval_input'
from /Users/tim/.rvm/rubies/jruby-1.6.6/lib/ruby/1.8/irb.rb:271:in `signal_status'
from /Users/tim/.rvm/rubies/jruby-1.6.6/lib/ruby/1.8/irb.rb:155:in `eval_input'
from org/jruby/RubyKernel.java:1408:in `loop'
from org/jruby/RubyKernel.java:1181:in `catch'
from /Users/tim/.rvm/rubies/jruby-1.6.6/lib/ruby/1.8/irb.rb:154:in `eval_input'
from /Users/tim/.rvm/rubies/jruby-1.6.6/lib/ruby/1.8/irb.rb:71:in `start'
from org/jruby/RubyKernel.java:1181:in `catch'
from /Users/tim/.rvm/rubies/jruby-1.6.6/lib/ruby/1.8/irb.rb:70:in `start'
from /Users/tim/.rvm/rubies/jruby-1.6.6/bin/irb:17:in `(root)'
jruby-1.6.6 :010 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment