Created
February 21, 2012 15:54
-
-
Save tlossen/1877125 to your computer and use it in GitHub Desktop.
playing with cloby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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