Summarized using AI

JRuby Polyglot Heaven

Thomas E Enebo and Charles Oliver Nutter • September 29, 2011 • New Orleans, Louisiana • Talk

JRuby Polyglot Heaven presents the integration of JRuby, a Ruby implementation on the Java Virtual Machine (JVM), with various JVM languages. The speakers, Thomas E. Enebo and Charles Oliver Nutter, discuss how JRuby allows developers to leverage multiple languages in their applications for enhanced capabilities. They emphasize that while Ruby excels in many areas, using other languages can sometimes provide better solutions for specific tasks.

Key points discussed include:

  • JRuby's Evolution: The talk commemorates the 10-year anniversary of JRuby, highlighting its growth with over 14,500 commits and significant community support.
  • Polyglot Programming: They stress that programming has become inherently polyglot due to the necessity of using different technologies in modern web applications. JRuby makes it easy to interact with languages such as Clojure, Scala, and JavaScript through simple installation methods and a shared runtime.
  • Integration with Other Languages: The speakers explain how JVM-based languages can interoperate seamlessly. This is primarily due to sharing a common garbage collector and memory management, which eliminates the complexity usually associated with inter-language communication. They showcase how Ruby can call Java methods and pass blocks between languages.
  • Specific Language Features:
    • Clojure: Focused on concurrency with immutable data structures and software transactional memory. They provide an example of using a persistent data structure in Ruby integrated with Clojure's functionality.
    • Scala and Actors: A demo illustrating Scala's actor model shows how Ruby can interact with it through a friendly syntax.
    • Rhino JavaScript: They explain how JavaScript can be executed in a controlled manner within Ruby applications, enhancing server-side capabilities.
    • Mirah: Presented as a Ruby-like syntax for compiling Java bytecode, enabling performance gains while maintaining a familiar syntax for Ruby developers.
  • Practical Use Cases: Several practical examples are provided to demonstrate how JRuby can be used alongside these other languages, enhancing the development of applications. For instance, using Neo4j with JRuby showcases its ability to interface effectively with graph databases.

The conclusion emphasizes that by using JRuby alongside other JVM languages, developers can effectively harness the strengths of each language within a single application framework, driving innovation and productivity.

JRuby Polyglot Heaven
Thomas E Enebo and Charles Oliver Nutter • New Orleans, Louisiana • Talk

Date: September 29, 2011
Published: December 12, 2011
Announced: unknown

JRuby is a top-notch Ruby implementation. It's also your gateway to polyglot heaven. Ruby can do anything, but it's not always the best tool for the job. With JRuby, you can take advantage of other JVM languages. Build part of your application in Clojure, taking advantage of its immutability and transactional guarantees. Build another part in Scala, leveraging its flexible static type system and actor libraries like Akka. Use JavaScript via Rhino to test pages or run JS on the server alongside Rails. Use Mirah to build fast-as-Java algorithms in a Rubilicious syntax. The JVM is a haven for polyglots, and JRuby's your ticket to ride.

RubyConf 2011

00:00:17.000 hello everybody good morning I think this will
00:00:23.960 be we were still working on our slides late yesterday but we're all set to go uh I think this will be my sixth time
00:00:30.679 presenting at Ruby C uh seventh time attending uh we're going to talk it as we did last year we decided that we
00:00:36.960 weren't going to do just a general J Ruby talk because most people can find those talks online find information about us and it's Ruby that runs on the
00:00:43.920 jbm it's really not a whole lot that we need to say usually so we thought we'd do something a little bit different and talk about J Ruby combined with a bunch
00:00:50.760 of other jvm languages and so we have J Ruby polyglot heaven today I think it would be safe to say we stretched
00:00:56.600 ourselves a little bit doing a polyglot talk yes exactly we don't actually know any of these other languages all that
00:01:02.359 well so um it'll be it'll be interesting once we get to some of the code stuff
00:01:07.600 but uh before we get going we do want to commemorate the fact that the this month or next month is basically the 10y year
00:01:15.000 anniversary of the beginning of J Ruby uh 10 years ago 2001 Yan ARA Peterson
00:01:20.320 started the first version of J Ruby here's here's all of the different J riew versions we've had over time uh 165
00:01:27.159 which will probably come out after we do Java 1 next week will be the 50th official release of J Ruby so very
00:01:33.479 exciting that we've been going so strong so here you go uh September
00:01:40.960 September 10th 2001 the the initial revision of J Ruby was originally committed to the repository uh followed
00:01:48.040 the next day by the initial check-in um I'm not exactly sure what the difference between those two were but it all
00:01:53.240 started at that point and slowly over time more and more contributors contributors came contributors went and
00:01:59.439 we're here today was the first one the read me yes perhaps it was just the readme file or the initial repository so
00:02:05.799 we've had hundreds of contributors over the years uh when I checked earlier this week we had 14,000 over 14,500 commits
00:02:14.040 over that amount of time which is actually very similar to the the MRI repository in its entire lifetime uh
00:02:21.080 thousands of users worldwide many more than we know lots of interesting ones that we do know and thank you thank
00:02:27.800 rubyists and thank jist around the world for helping to make jrb uh what it is
00:02:33.080 today so right now we have jb6 is the current version we've got a 164
00:02:38.280 maintenance release that's the one out right now uh we added Ruby 1.9 support which is you know in the neighborhood of
00:02:45.560 80 to 90% of stuff is there there's more work that we need to do and we need more tests and more specs uh but it's it's
00:02:51.959 pretty solid and there are people running with it now um just a quick hand is anyone using one nine in production
00:02:58.120 with J Ruby well we got we got a few hands anybody running run night production on MRI now
00:03:05.440 it's getting there definitely picking up over the past year it's it's definitely become much more likely that people are
00:03:11.239 running one nine um it's still kind of ramping up though we added uh experimental sea extension support in
00:03:17.640 this release thanks to Tim felgen shref and the ruby summer code 2010 which was an incredible effort and actually a
00:03:24.760 large number of extensions do work there's some limitations when they run on J Ruby but uh as a migration strategy
00:03:30.720 a lot of times you can install those gems and they'll work until you can find a more compatible option uh hundreds of
00:03:37.280 fixes in that release thousands of commits it was every every big release we have seems to be even bigger than the
00:03:42.920 other one uh and I think this six was actually bigger than 14 and 15 combined
00:03:48.239 so there was a tremendous amount of work that into went into it and I mentioned 164 is the current maintenance release
00:03:53.319 165 comes along soon with some more One n fixes and other stuff uh get our plugs out of the way uh
00:03:59.040 engine yard cloud now officially supports J Ruby it's not in beta anymore so if you're familiar with the engine
00:04:05.079 yard cloud or if you want to be familiar with the engard cloud you can just drop J Ruby applications in there with all
00:04:10.400 your Java libraries with whatever other whatever languages and libraries you want to use from the jvm and it will
00:04:15.920 just run like anything else in the cloud and you can uh go to try Ruby trj Ruby at the engine yard.com for the 500 free
00:04:23.240 hours uh or check out our blog for more information about it byy it yes and
00:04:28.639 here's the book uh the book also came out this year we finally got it finished and released it this past winter um I
00:04:35.320 will be tossing a copy into any giveaway that might be done as part of Ruby conf today uh so hopefully one of you will be
00:04:42.479 able to get a free one uh but if not you can go ahead and pick up the book and let us know what you like and if you'd
00:04:47.919 like to see something new in a second edition okay so now the talk begins um I
00:04:55.600 don't think any of any of us would be here if we didn't love Ruby but I think we all acknowledge that some languages
00:05:01.840 do things better than Ruby for some things so um there's good reasons to use
00:05:09.039 additional languages and actually we're already all polyglot programmers to begin with because you just have to
00:05:16.560 write one web application you just use a whole plethora of Technologies and the goal of the talk
00:05:22.840 today is to actually show that it's it's quite easy to use another language from within J
00:05:28.479 Ruby and in fact uh it's super super easy to install this is one of our
00:05:34.160 favorite features as of late um Maven is this really crappy build tool but uh it
00:05:41.560 it has an incredible number of um jars published to a
00:05:46.800 repository and if you want to install any of the jars from that repository just do mvn colon um and you specify the
00:05:54.520 group ID or the association that published it and then it's artifact ID or the name of the jar so pretty much
00:06:01.479 any of the tens of thousands or hundreds of thousands of J of of jbm libraries that are out there are kind of just
00:06:07.720 transparently installable as gems on JB and there's like 200,000 jars out there it's like every version of every jar
00:06:13.960 ever released but how weird is that um if you want to actually go and find one of these libraries you can go
00:06:20.319 to mvn repository. comom put in your search string and then the group ID and
00:06:25.560 artifact ID is just right there um I think this took Charlie about 20
00:06:31.720 seconds to fill in this table so it was actually quite easy to find this stuff and these are going to be the languages
00:06:37.560 that we're going to be talking about today as well as Java obviously as well as Java it's it's tough to avoid Java um
00:06:45.479 and J Ruby yeah um so U there are a couple problems with Maven support at
00:06:52.000 the moment um if you wanted to make your own Gem and push it to rubygems.org um
00:06:58.160 rubygems.org doesn't allow external dependencies so we need to fix this also
00:07:05.080 unfortunately you can't put a maven dependency in your gem file um so we need some help uh fixing
00:07:12.039 bundler to allow this as well so if this sounds really interesting and fun please
00:07:17.199 come on talk to us after the talk or email us or send us carrier pigeon I don't know
00:07:25.960 um so um polyglot programming is quite a bit nicer on the jvm than it is
00:07:31.680 ordinarily because all these languages are built on top of the same base or the jvm they have the same garbage collector
00:07:39.160 they use the same thread scheduler everything gets compiled down to Java B code so if you um want to run
00:07:47.520 on open BSD crazy Edition you don't have to like compile three different language run times you know you just have the the
00:07:54.120 jar files and you put them there and they run um the other thing worth mentioning is we're only showing four
00:08:01.000 languages or five if you count Java but U um there's over a hundred languages
00:08:06.240 that people have implemented some of them are toys but there's many other serious languages that are out there as
00:08:13.199 well um I just wanted to underscore the the point about having um common a
00:08:18.919 common base of the jvm in particular the GC so here's an example showing C Ruby
00:08:25.280 let's say you go and Ed j9 through a native C extension and and like any
00:08:31.080 application that gets reasonably complex it's been running for a while and you have a memory leak well you hook up to
00:08:36.839 your memory um your memory tools um in Ruby and maybe you find it but if you
00:08:42.839 don't then what is this uh is this memory fragmentation because uh MRI
00:08:47.920 doesn't have a compacting collector is it in fact JavaScript and and in this
00:08:53.600 scenario maybe you can hook up to the JavaScript engine and figure it out and maybe you can't but it leads a big cloud
00:09:00.200 of mystery that I don't think most developers would actually want this is also one of the downsides of C
00:09:05.600 extensions because C extensions have their own their own memory management and they do things their own way oftentimes you can't see where where our
00:09:12.920 magic has managed to stuff that that four megabytes of memory that is never never going to come back uh having it
00:09:18.880 all be on the same runtime and the same object model and the same GC it's all available it's all inspectable
00:09:24.519 everything is there and and you don't have that mystery and we even have a term for it and it is the our Magic problem
00:09:31.760 yes um if you actually go and run in the jvm um the JavaScript implementation
00:09:38.120 Rhino is is based at Java Lang object just like J Ruby is it runs in the same
00:09:43.880 collector now if you have this memory leak you can go and hook up to a j visual VM inspect the uh the Heap and
00:09:51.040 it'll be really obvious that there's something that's holding on to too much memory in JavaScript and for example if
00:09:56.320 you're using tools that will walk the Heap and show which objects are referencing other objects you'll see Ruby objects that reference JavaScript
00:10:02.880 objects that reference Scola objects because it's all just objects in memory Parts is Parts um a lot of people
00:10:10.920 find that communicating between languages is difficult and this certainly can be true and we're going to
00:10:16.360 make some recommendations that will make it a lot simpler on Java um we did actually spend an immense
00:10:23.720 amount of time making um Java feel like a first class citizen from within Ruby and and Charlie's going to be giving a
00:10:29.920 demo of that in a in a couple of minutes um and Java can call back into Ruby um so we have this nice
00:10:38.160 bidirectional communication with this language but what if we decided that it was
00:10:43.959 important to support closure um we could certainly go through the same amount of work again but as a language implementer
00:10:51.040 um as more and more people express interest in supporting different languages this becomes pretty unscalable
00:10:57.079 for us in fact it might not even be possible for us because some of these languages syntaxes are exotic enough
00:11:03.959 that they just wouldn't fit into a ruby syntax so they can't really be first class citizens however every language um
00:11:11.880 written on top of the jvm wants to be able to use Java libraries so they have
00:11:18.279 good communication with the Java language itself so what we recommend is using Java as The Interchange format for
00:11:26.519 communicating between languages and particular particular um we recommend
00:11:31.880 constraining the use of what you passed to Java for this communication to be simple primitive types there's one odd
00:11:39.600 man out here and that's object which you can essentially use as an opaque reference or a pointer so you could pass
00:11:46.720 a ruby object to Scala Scala will never do anything with it except to hand it back to Ruby later to have Ruby do
00:11:52.600 something with it but basically simple types and maybe one pointer um all these languag
00:11:59.800 also can um Implement interfaces and call methods on an interface so what we
00:12:05.920 recommend is um doing all communication through an interface and then this encourages you to create essentially a
00:12:13.480 coar grain contract because you're crossing a pretty big barrier going between two languages so we find this to
00:12:19.880 be a reasonably simple strategy and uh you'll be able to manage
00:12:26.880 it better over time because you have to go through the pain of recompiling an interface every time you want to change
00:12:32.079 it just as in any complex system you want to treat the those different pieces in the different languages as their own
00:12:37.560 components toine a nice interface in contract and use that as your way to interface rather than doing raw method
00:12:43.040 calls across the boundary all the time from one language to another we like beer yes does anyone recognize what
00:12:49.760 these are yes yes they are they are keg uh
00:12:55.480 connectors for a tap all right so of course we can't we can't talk about integrating with languages in the jvm
00:13:01.360 without at least brushing across Java here uh and so really we're what we're
00:13:06.680 talking about here is that it's the primary jbm language with thousands of libraries really all the libraries are just bite codes So when you say that
00:13:12.639 there's a lot of java libraries you're really saying there's lots of jvm libraries regardless of what language they're written in uh but the base J jvm
00:13:19.720 types that Java uses heavily are kind of the the lingual franka for doing interop
00:13:25.120 between all these different languages the strings the Primitive numbers uh basic object references interfaces
00:13:31.519 that's the the cleanest way for languages on the jbm to interop rather than trying to do it at some higher
00:13:36.720 level uh and of course it's very straightforward to use from J Ruby so let's take a look at the very simple
00:13:42.600 example uh just a dumb class with uh has one public field a private field some
00:13:48.240 accessors uh a method at the bottom do something that takes an interface a runnable uh so we've got this Java class
00:13:54.839 and we want to interact with it from Ruby uh it's very trivial to do uh require in Java support so we have
00:14:01.560 support for all the J jbm types and Java interrup uh we import that class which
00:14:06.880 will basically just assign a constant in the current scope the current context pointing at that class and from there on
00:14:12.839 it pretty much is Just Ruby code construct a new instance of it passing in whatever Ruby arguments arguments
00:14:18.199 we'll figure out how to try and map that to the Constructor or give you an error if it doesn't match uh accessors for
00:14:24.160 example here get name uh we turn it into more of Ruby accessor Styles so you can do simple. name or simple. getet uncore
00:14:32.279 name or the full-on Java like get a capital N name again with the set name
00:14:38.519 we just turned it into sort of a ruby attribute accessor and so you just assign it right into the object and it looks like a regular Ruby Ruby call
00:14:45.279 rather than littering uh Java likee code all over your Ruby code base uh public Fields will also be accessible uh and we
00:14:51.959 have some some puristic to manage which public fields are exposed and which methods are exposed uh down at the
00:14:58.120 bottom here we have do something and uh again with with the intent of keeping it
00:15:03.600 feeling like Ruby Whenever there is the camel casing in Java we'll throw an underscore in there so that you can call
00:15:09.160 it with the normal Ruby like name and it keeps the code looking a little bit more clean uh more interestingly here though
00:15:15.440 is that fact we're actually just passing a block into do something J Ruby will if the last argument to a call is a regular
00:15:22.040 Java interface accept a block and just make that block magically implement the interface for you so all of these
00:15:28.160 patterns that are ESS like passing in a closure or a block that's exactly how you do it in
00:15:33.360 Ruby a little bit more concrete example uh popular Java based uh graph database
00:15:39.120 Neo forj uh you can gem install Neo forj and try some of this out and these examples are actually just lifted right
00:15:44.759 from the gem a high performance nosql graph database uh being used for for a
00:15:49.880 lot of a lot of applications more and more applications these days now that no sqls uh still in in Vogue uh so Neo forj
00:15:57.199 is the gem and that will pull down Neo forj jar as as as well as some wrappers
00:16:02.440 that go around it which are essentially just written in Ruby using our our integration layer so here's a quick example we have
00:16:09.560 our little model object a person we include the Neo forj node mix in so it
00:16:14.839 becomes a node within the Neo forj graph database uh we've got a couple properties named here we have some
00:16:21.319 relationships uh so it has N Friends for this person we've got some Lucine
00:16:26.399 indexes which is just built into Neo forj for doing faster searches across the the graph uh and then a simple 2s
00:16:32.759 that we Define in Ruby just to show off what the person's name actually is now using this here's a little
00:16:39.519 command line tool uh NE forj will persist back out to the dis every time the the process ends so this will run
00:16:45.360 and repeatedly update the same database we have a Neo forj transaction. run we
00:16:51.600 pass that a block within there we can create models modify models delete
00:16:56.680 things do walks over the uh the graph and when the transaction is completed it will commit it and push it out to the
00:17:03.240 database that's on disk here's one where we actually go and find what the actual doing a query for
00:17:10.480 the person that we want to to locate uh passing in a key and a value person. find similar to an active
00:17:17.640 record sort of model will do the lookup and walk the database and find where those where those persons
00:17:25.199 are and then of course simple things like being able to walk all of the different object object that are in the graph database all nodes. each and just
00:17:32.240 walk and check each of them uh interestingly here Neo forj will pass you the ref node when it's done walking
00:17:38.720 the graph so you know that you've actually completed the uh the full walk a so off from Java on to JavaScript
00:17:48.600 so we couldn't actually find a JavaScript logo I don't think there is an official JavaScript logo is there but
00:17:54.280 it's such the rage yes people not only use it on the client they now use it on the server to have a logo right right no
00:18:00.679 Dutch JS um well about a year ago we went and saw a really cool demo um of
00:18:07.280 someone who had a a very specific service side need uh he had a server and
00:18:13.799 for the life of me I can't remember what the service was but it allowed users to
00:18:19.760 go and uh upload Snippets of JavaScript that ran on his server but he was really
00:18:26.080 concerned because people would make mistakes and being some while loop and then essentially create a denial of
00:18:31.320 service attack he was also worried about people you know hacking the system calling outside of that script engine
00:18:39.000 and executing a process Rhino for the win uh um because
00:18:45.679 it runs on Java you can set up a Java security policy and restrict their ability to go and call
00:18:52.559 out to those various apis like executing processes but even more interestingly
00:18:57.600 Rhino has capabilities for monitoring the execution of JavaScript
00:19:03.240 itself and I we're reusing his demo we're actually reusing a lot of people's
00:19:08.840 demos today if if you're in the audience thank you
00:19:14.000 um ah super awesome um that requires how you actually load the maven gem that we
00:19:20.080 were talking about earlier um the top snippet we're actually using um Rhino's Java API to go
00:19:26.799 and set up the um the Run time instance we're setting up a context to evaluate
00:19:32.159 things on and then a basic uh object scope in JavaScript itself and down
00:19:37.880 below um we're just executing that string code against that
00:19:44.039 scope um uh so I didn't know how to actually print out stuff in JavaScript but then I
00:19:50.760 realized that you can in fact to um go and call Java code so I'm like ah out.
00:19:56.520 print line sweet and then we we were talking about this yesterday I'm like ah Ruby's calling
00:20:02.640 Java API to set up JavaScript which is calling Java well what if we actually used uh
00:20:09.159 embed scripting context we could call back into Ruby again yes sorry I didn't do it for the demo
00:20:16.600 but it's uh it's just a sweet thought um there was two things that had
00:20:23.720 to be set up uh um one was a restricted context which allowed you to go and capture the time that the script started
00:20:30.159 executing along with how many JavaScript instructions were executed and then a context Factory and
00:20:38.159 there's really only one method worth looking at here and it's observe instruction count periodically it'll go and pass in
00:20:46.120 that restricted context and how many instructions had uh um been executed since the last time um this method was
00:20:54.600 called and then you can just see the if statement's very simple if we haven't uh
00:20:59.760 executed more than a thousand statements or it hasn't been running longer than five seconds um you're okay but if you
00:21:07.799 have then it raises and if we run it it made it
00:21:14.240 through seven print statements and a thousand instructions so I really have no idea the granularity of what
00:21:21.279 instructions are in Rhino but there must be a lot of them so doing seven iterations was it crossed the 1,000
00:21:27.520 instruction limit and JavaScript so but as far as I know um no other JavaScript
00:21:32.559 implementation has this Resource Management Facility so it's it's a pretty cool feature right right exactly
00:21:38.400 the ability to be able to sandbox a piece of JavaScript code and allow users to submit an arbitrary piece of JavaScript code all because we're
00:21:44.679 running on the jbm we have the security policies and because this implementation is able to to to watch how much
00:21:50.000 instructions are being executed so closure closure fans in the crowd here cool a few of you all right so closure
00:21:58.320 um this is my description I think it's a nice short description it's kind of a list for the jvm that's focused on concurrency making concurrency easy so
00:22:05.600 it's immutable by default uh you can mutate things but there specific ways you have to do it there are what's
00:22:11.000 called persistent data structures I'll explain what that is in a moment uh as well as software transactional memory
00:22:16.080 one of the the first languages to really popularize software transactional memory at least on the jvm uh and again if you
00:22:22.720 want to play with closure it's very easy you can just gem install the the maven Library it also popularized lisp on the
00:22:28.520 jvm yes I don't think it had a whole lot of impact before that uh so persistent data structures here's kind of the
00:22:34.320 canonical closure diagram for persistent data structures uh what we have over there on the left side is a large graph
00:22:42.320 of objects this is essentially a tree in memory that represents for example a hash or a list uh and what we want to do
00:22:49.120 is we want to add a node somewhere now persistent means that most of the nodes
00:22:55.600 from the previous collection are still car across and still referenced in the new collection only the path we you need
00:23:02.919 to modify through that collection is new in the in in the new version of it so it reduces how much object churn you have
00:23:10.279 you don't have to recopy the entire tree of objects uh and it it still preserves
00:23:16.279 all of the same immutable constraints you always know that the copy of the collection you have in hand is not going
00:23:21.640 to mutate out from underneath you it's going to look exactly the same as long as you're holding that collection reference and all old references are
00:23:28.720 still valid as long as something has it exactly all old references stay there if they happen to fall off then they just
00:23:34.000 go out and they're normally garbage collected as as regular jvm objects uh so here's an example of using
00:23:39.840 some of closures we're kind of cheating with closure here you can go use the language of course and and it's it's lisp and it has these facilities but the
00:23:46.279 interesting bits are really the the libraries the built-in stuff that comes along with closure which you can use as
00:23:52.159 just a normal Java Library here we're importing closure Lang persistent hashmap which is their their version of
00:23:57.960 a map uh so we have a little Ruby hash that we'll use to prime it uh we construct a new persistent hash map and we get our
00:24:05.039 closure hash out of that printed out pretty much looks the same as any other map uh we're going to iterate across all
00:24:11.679 those and again it works the same uh selecting same sort of thing we're going to turn it into a series of arrays but
00:24:17.760 then more interestingly what let's actually try and modify this collection because this is an immutable collection
00:24:23.960 and you need to always get a new version of The Collection out uh you'd get an unsupported operation exception raised
00:24:29.919 so you cannot mutate this directly that's part of the guarantee no one else can mutate it either you know the
00:24:35.120 collection's always going to look the same what you actually need to do instead is call the asok method to do a
00:24:40.760 new Association in this map which returns a new reference to a collection
00:24:45.880 so over time you will churn through a couple of these top level nodes and make some minor modifications but for large
00:24:52.440 graphs large maps and lists you'll save most of that content across all of those changes while still having all the full
00:24:59.000 immutable uh semantics preserved so then there the STM software
00:25:04.399 transactional memory basically software transactional memory enclosure gives you a transactional ref object that ref
00:25:11.320 points at one object somewhere in memory uh in order to modify what it points at you need to start a transaction using
00:25:18.480 the do sync call do sync call wraps that piece of code in a little closure essentially uh and then within there you
00:25:25.559 can modify as many refs as you want and it will try to commit those changes once the transaction is over if during the
00:25:32.559 time that the transaction is running somebody else manages to commit theirs first it'll just rerun yours again so
00:25:38.120 there's a potential for some churn if you have lots of threads contending for the same transactional refs but in
00:25:44.399 practice that doesn't happen a whole lot usually someone will win then someone else will see the new changes make their
00:25:50.039 own modifications and proceed from that point and again uh being able to use
00:25:56.440 this all from Ruby here is basically Ruby code using closures STM we import
00:26:01.799 uh closure Lang ref and locking transaction we're going to create a new ref that just points at a string to
00:26:07.480 begin with uh you can see that you dreference it and we get those the string out and then we'll try and just
00:26:12.880 set it directly without a transaction you get an illegal State exception you can't modify a ref without this do sync
00:26:18.600 transaction wrapping around it uh so then what we'll do is we'll take a locking transaction tell it to run in
00:26:24.720 that transaction another set to the ref that transaction will actually succeed and be committed to the ref and then
00:26:30.880 dereferencing it later will will actually show the new value uh and uh notice here this is this runin
00:26:36.279 transaction is actually capital I capital T runin transaction Java method
00:26:41.640 on the closure library and we're passing a normal Ruby block to it it runs the Ruby block in the transaction just as
00:26:47.159 you'd expect it would so Koby is a little library that I
00:26:53.399 wrote that kind of combines all of this and makes it just be like normal Ruby uh combines the persistent data structures
00:26:59.840 it uses one of the persistent list forms it com it takes the this software transactional memory and plugs it into J
00:27:07.200 Ruby as just a a transparent part of the Ruby language and what you actually get out of this is transactional instance
00:27:14.320 variables so you can going to extend from a closure colon colon object and All rights that you do to that object
00:27:20.919 have to be done within a transaction but you're guaranteed that those instance variables will never be mutated without a transactional semantics behind it very
00:27:28.840 nice little toy um I don't have it's on my machine so we'll switch to the end for the demo uh if we have a little bit
00:27:35.159 time to to show that kind of fun to play with and uh I'd like to see more of this kind of stuff done more experimentation
00:27:41.000 with this on uh J Ruby oh that wasn't for Scala that was
00:27:46.919 for Scala fans here there's a couple
00:27:52.440 um so I'm not a scholar programmer but um we came up with the aor which we
00:27:58.679 think will be our contribution to Scola yes I think this will take off it's an omni Paradigm programming language um
00:28:05.720 it's objectoriented it's functional um it's got a rich type system and every
00:28:11.760 other thing that you've ever heard at any computer science class you've ever been to yes it's all in there everything
00:28:17.600 is there it's the it's the everything language um what we're going to show a
00:28:23.159 demo of is is ma which is a nice Ruby wrapper around AA which is a popular um
00:28:31.679 actor framework in Scala and in a sense we're sort of having our cake and eating it too because we don't actually have to
00:28:38.960 learn any Scala to make use of this very useful schola
00:28:44.640 Library um the demo that we stole was from Alise thank you um but it was
00:28:50.720 ported from using Java integration directly calling aa's uh Java Library
00:28:56.480 support and ported to have a nice Ruby
00:29:01.799 syntax um there's four actors there's a conductor that receives a a band list
00:29:07.840 and then it uh um sends start messages to each of the uh um each of the
00:29:12.880 instruments and then when one of the instruments is done playing it sends a request back to the conductor saying let
00:29:19.559 me play my end my end note and then it says oh okay and then it plays it this
00:29:24.919 is a weird demo um um the first thing you do is you
00:29:30.480 actually have to get references to your actors and I have to ask this question although it has nothing to do with
00:29:35.559 polyglot programming how many people like tap okay good there are a few people
00:29:42.600 there's a few how many people dislike tap okay a couple okay well you guys can
00:29:48.000 leave the room now um um so I use tap to go and put this
00:29:55.000 start on one line deal with it um okay so we have a simple struct called
00:30:02.640 band and then we put the the three instrument actors into it and we pass it
00:30:08.640 using the send one way method in AA you'll notice that it's less than less
00:30:13.720 than this is an illustration of having a friendly Ruby
00:30:19.799 syntax um and then we sleep for some amount of time and then ask all the actors to stop
00:30:26.760 acting um in the conductor it receives two message um you can see that it
00:30:31.880 extends MAA actor which is actually an AA actor if I'm pronouncing AA right um
00:30:38.600 but the first one is the roster in which case we do another send one way to each of the instruments and then it also
00:30:45.519 receives an and and then goes oh okay yeah you can play the end and then replies back with
00:30:51.279 and um the instruments uh um if they receive a start they play an MP3 um B
00:30:57.960 based on their name guitar. MP3 if it receives an end then it's
00:31:03.399 guitar .mp3 and um in fact drum actually
00:31:10.279 overrides this play method to send the end request and to totally drive that thing
00:31:16.840 home uh and the the diagram is wrong which I realized this morning but I thought we
00:31:22.480 were going to use Charlie's slid so I didn't bother to update them um BAS
00:31:28.440 will send an end message back to conductor and then conductor says yes and then it plays base so if I can have
00:31:36.120 Charlie be the hands here and then I have an ulterior motive for this
00:31:41.159 uh go into wonky music Ma just hit return
00:31:46.440 once it's pretty funny because he doesn't use emac and I'm using shell
00:31:52.039 mode um so J Ruby D- 1.9 play
00:31:59.200 and put the mic down there
00:32:13.960 to yeah it doesn't it is it is a terrible conductor I would not recommend that you use this for your music
00:32:19.440 generation but you know the other thing I really love about this demo is it's really long it's like 40 seconds
00:32:29.440 there awesome oh no it's not done wait wait for we're we're halfway
00:32:44.600 done yeah all right
00:32:51.720 sweet the real the best part about that demo is we could do that as a a talk about how concurrency works and how you
00:32:58.080 don't know how things are going to be delivered in order uh and it would be exactly the same demo um so one last one
00:33:05.159 last point to this demo is this was all using a ruby syntax but you could have certainly used the features of Scala and
00:33:11.799 wrote some of your actors in Scala and taken advantage of one of the thousands and thousands of features of Scala
00:33:25.440 real quick is uh Mera mea's another little language that I've worked on some of the time Nick Howard here has also
00:33:31.080 been taking over a lot of the the work in and Bug fixing lately and I have to do my fancy animation
00:33:37.480 here yes what just happened oh isn't that a magical we got to do that one more time yes that that was one of my
00:33:44.159 Saturdays I think it looks different now was was creating my little ambigram there um okay so yes and you know I'm
00:33:50.799 available for all sorts of other events as well so Mera uh mea is basically a
00:33:56.200 ruby like syntax for creating Java or jvm bite code uh it is not Ruby uh that
00:34:02.399 is the the common misconception mirror does not run rails it is essentially just a ruby Syntax for writing jbm bite
00:34:09.359 code that's all there is to it uh it's very clean and beautiful and simple like Ruby and what you get out is plain old
00:34:16.399 Java code it can just generate Java code for you or jvm bite code so if we take a look back at our our lovely Java example
00:34:23.720 of a simple class here uh this is essentially the equivalent in mirror uh
00:34:28.760 generates almost identically the same code in either a Java Source form or a jvm bite code form so it's it's intended
00:34:35.879 to try and give some of the Ruby feel at least The Superficial Ruby feel while still having the the straight up static
00:34:41.839 type semantics and and raw Java code and raw Java performance uh that you want when you run on the
00:34:47.800 jvm uh so that's that's the simple example and interacting with this class would be exactly the same as the Ruby
00:34:54.879 code we saw before it is just the normal Java class all of the code that worked before essentially works the exact same
00:35:00.560 way with this uh so perhaps playing around with a little bit more something a little bit more interesting the final thing here uh there is a Java inline gem
00:35:08.720 it basically is a plugin for the Ruby inline gem that allows J Ruby to embed into Ruby code any number of languages
00:35:15.720 and they just embed as if they're a regular method uh so it currently supports Java Mira and Scala other
00:35:21.800 languages would not be difficult to add to it and it is all written in Ruby so it just calls out to those libraries
00:35:27.240 compilers dumps it into the Ruby class and you've got a method in your Ruby class that is implemented in any one of these
00:35:33.000 languages uh the caveat is I don't think anyone actually uses this gem but maybe it'll be interesting for somebody in the
00:35:39.160 future so here we have our our our we're we're starting our new startup that is going to be the fastest factorial and
00:35:46.160 FIB generator of anybody so this is this kind of a it's kind of a wolf beta that we're going to give out to the world and
00:35:53.200 so here's the Ruby versions and uh we've got our Ruby versions and we go to market and it's it's working great but
00:35:58.640 it turns out it's not quite fast enough we're not generating those Fibonacci numbers as fast as we'd really like to
00:36:03.920 so we thought we'd try a few other languages and throw them in here so here we we'll just do one in Java uh this is
00:36:09.880 pretty much the same Ruby inline syntax inline Java uh we'll set up a package for the Java code to go into and then
00:36:16.720 just have raw Java code as a string right there in the code this actually will then Define a factorial Java and a
00:36:23.599 fib Java method on whatever the current class body is and when you call those it will just be calling straight out to the
00:36:29.640 Java version again another one in Mirror the one in mirror is a little funny because it it almost basically looks like Ruby
00:36:36.560 code that's embedded in a string in Ruby code uh but it is statically typed and it becomes essentially what the Java
00:36:42.599 code would do and then of course we have our Scala version uh which are it's it's
00:36:47.839 a lovely beautiful on line of unintelligible code but there it is and
00:36:52.920 if you like Scala you can embed that just as easily so another simple little way that you can do language integration
00:36:58.599 with J Ruby and I think that's about all we have um we went we started a little late
00:37:04.040 so we'll we can delay you all for lunch if there are questions you'd like to hear we also have in the back Shane is
00:37:09.520 is guarding the uh the J ruby shirt table so everybody in the room should be able to get one and uh and then
00:37:15.280 everybody else that's not here they'll just have to pick up whatever's left uh and we have time for questions so questions about any of this yes
00:37:28.079 for this particular Benchmark they actually end up being basically all exactly the same because at at the jvm
00:37:33.400 level they all just compiles down to the same bik code and optimizes the same way so
00:37:40.440 what yes the schol one does it's magical yes so um I I know I'm asking to predict
00:37:46.560 the future here so whatever but um do you I have heard talk there they are looking at adding real support for
00:37:52.560 selfer transactional memory to the jbm rather than just being a language dependent feature um if that happens do
00:37:58.839 you think there would be a possibil seeing you know real STM in J Ruby itself so the question is that there's a
00:38:04.560 possibility they might add STM to the jvm itself um certainly if they anything that they add to the jbm we can we'll
00:38:10.520 take advantage of and things that they have not officially added to the jvm we even take advantage of um so yes we
00:38:15.960 would love to see that and we'd love to have raw VM support but in the interim there's things like closure and clobe
00:38:21.599 that can give that same sort of logic to you and it will be exposed as a Java API so we'll be able to use it on
00:38:28.119 of course by that means one more in front here uh the trend of people committing to J do you see it Rising I
00:38:35.160 mean like is there a lot or is it still ising well actually I so the question was about the the trend of committing
00:38:41.400 and contributions to J Ruby uh it seems to endlessly go up uh actually I just
00:38:46.680 from commits for the the core folks who there's about a dozen of us uh I noticed over time that it's over the past 52- we
00:38:53.400 trend on gith up it's still continuing decline uh but we're also getting a ton more pull requests and patches coming in
00:39:00.000 from people so it's continually growing yeah I'd say we get a lot more oneoff of pull requests and that continues to grow
00:39:07.520 consistently we'll take questions till they kick us off how about you right over here uh in Cas like closure which
00:39:13.560 is meant for doing things in parallel uh if you have something like you're creating a closure to pass to a sequence
00:39:20.640 um that you want to iterate over what happens how how do you support parallelism if your host environment is
00:39:26.640 Ruby have these bindings over here and you're trying to send it to something that would allow that right so a
00:39:33.520 question about passing for example of Ruby closure uh Ruby block we'll say so
00:39:38.560 we don't get the confusion a Ruby block into closure to do su iteration um anytime closure interacts with anything
00:39:44.480 that's not closure you have the potential for concurrency semantics to be broken uh so you would have to guard
00:39:50.240 that logic within your Ruby implementation to make sure you're not doing things that would be bad for concurrency um but otherwise it PR just
00:39:57.720 will call functions that you pass in and so it is pretty easy for you to take a piece of Ruby code and pass it to
00:40:02.800 closure and just have it run it's it's pretty much all the same design concerns you'd have with calling Java from
00:40:08.119 closure and with that I think we'll close it off you can come and grab us for other questions make sure you get a shirt on the way out and we'll see you
Explore all talks recorded at RubyConf 2011
+55