Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
JRuby Embed: instantiating RubyHash etc. objects
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marko Asplund  
View profile  
 More options Oct 27 2012, 11:34 am
From: Marko Asplund <marko.aspl...@gmail.com>
Date: Sat, 27 Oct 2012 18:33:16 +0300
Local: Sat, Oct 27 2012 11:33 am
Subject: [jruby-user] JRuby Embed: instantiating RubyHash etc. objects
Hi,

I'm using JRuby Embed API in my Java code, where I create an object
graph that is processed and then passed to Ruby code. This graph
contains e.g. RubyHash and RubyArray objects.
The Java code runs in a multi-threaded environment and I'd like to
make sure I'm not introducing any race conditions.

The application instantiates a single ScriptingContainer object, which
is shared among different threads.

sc = new ScriptingContainer(LocalContextScope.CONCURRENT);

RubyHash/Array objects are instantiated as follows:

RubyHash rh = new RubyHash(sc.getProvider().getRuntime());

Here the API requires that the Ruby runtime object is passed to
RubyHash constructor.
Is this code thread-safe?
Can RubyHash objects be instantiated in another way?

marko

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Yoko Harada  
View profile  
 More options Oct 27 2012, 12:01 pm
From: Yoko Harada <yoko...@gmail.com>
Date: Sat, 27 Oct 2012 12:01:22 -0400
Local: Sat, Oct 27 2012 12:01 pm
Subject: Re: [jruby-user] JRuby Embed: instantiating RubyHash etc. objects
Hello,

When you choose LocalContextScope.CONCURRENT model, thread-safety
depends on the Ruby code. If you want to make a Hash object
thread-safe, you should use java.util.concurrent.ConcuurentHashMap.
Java's map type object behaves like Ruby's Hash in Ruby code. For
example,

    ScriptingContainer sc = new
ScriptingContainer(LocalContextScope.CONCURRENT);
    ConcurrentHashMap myHash = new ConcurrentHashMap();
    sc.put("my_hash", myHash);

on Java side. Then, my_hash is available to use on Ruby side. See my blog post:
"4. New Map proxy" of
http://yokolet.blogspot.com/2011/01/embedding-api-refinements-for-jru...

The change on Java's Maps on Ruby side is done on Java side at the
same time. If you add some new key-value pairs in Ruby code, you can
see those pairs when you are back to Java code. This is because the
reference to the Map object is given to Ruby code.

-Yoko

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marko Asplund  
View profile  
 More options Oct 29 2012, 4:40 pm
From: Marko Asplund <marko.aspl...@gmail.com>
Date: Mon, 29 Oct 2012 22:39:14 +0200
Local: Mon, Oct 29 2012 4:39 pm
Subject: Re: [jruby-user] JRuby Embed: instantiating RubyHash etc. objects
Hi,

thanks.

I'm working with an existing Ruby code base that uses an opensource
Ruby module (Liquid) that doesn't work with data structures created in
Java code. The only workaround I've come up with is to convert the
Java data structures to Ruby structures before passing them on to Ruby
code.

So, if I'm sharing a single (concurrent) ScriptingContainer between
multiple threads the following piece of code doesn't expose the Ruby
runtime to race conditions if the Ruby code itself is thread safe?

RubyHash rh = new RubyHash(sc.getProvider().getRuntime());

The constructor call feels a bit verbose. Would it be possible to get
an overloaded constructor that would accept a ScriptingContainer
argument?

marko

On 27 October 2012 19:01, Yoko Harada <yoko...@gmail.com> wrote:

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »