How to Tell If I'm Running from Jruby VS. Ruby

How can I tell if I'm running from JRuby vs. Ruby?

I believe you can check the RUBY_PLATFORM constant.

Rails: How can my app tell if it is running in MRI or JRuby?

Are you able to distinguish between the two like this:

case (RUBY_ENGINE)
when 'ruby'
# ...
when 'jruby'
# ...
end

You could write a method to give you a jruby? method if required:

def jruby?
RUBY_ENGINE == 'jruby'
end

How do I tell which Ruby interpreter I'm using?

Nowadays, all mainstream Ruby implementations set the RUBY_ENGINE pseudo-constant. The values for the various implementations which I can remember off the top of my head are:

  • Rubinius: rbx
  • JRuby: jruby
  • TruffleRuby: truffleruby
  • Opal: opal
  • MRuby: mruby
  • YARV: confusingly, ruby
  • MRI: even more confusingly, also ruby
  • MagLev: maglev
  • IronRuby: ironruby
  • MacRuby: macruby
  • Topaz: topaz

JRuby on Rails vs. Ruby on Rails, what's difference?

JRuby is the Ruby implementation that runs on a JVM whereas Matz's Ruby is a C implementation.

Key features to note are:

  1. JRuby runs on Java VM's and it's either compiled or interpreted down to Java byte code.
  2. JRuby can integrate with Java code. If you have Java class libraries (.jar's), you can reference and use them from within Ruby code with JRuby. In the other direction you can also call JRuby code from within Java. JRuby can also use the JVM and application server capabilities.
  3. JRuby is usually hosted within Java application servers such as Sun's GlassFish or even the Tomcat web server.
  4. Although you cannot use native Ruby gems with JRuby there are JRuby implementations for most of the popular Ruby libraries.

There are other differences which are listed at the JRuby wiki:

  • Differences between JRuby and Ruby (MRI)
  • JRuby On Rails

JRuby on Rails vs. Ruby on Rails, what's difference?

The original answer still stands and isn't really time specific. JRuby is just a version of Ruby that runs on the Java Virtual Machine (JVM). If you're starting from scratch, just go for plain Ruby. JRuby is mostly useful to people who have existing Java code and would like their JRuby and Java apps to communicate.

How can I check whether a JRuby script defines a particular method?

When defining a global method, it becomes a private method of Object. You must therefore use the include_all parameter for respond_to? to get this to work:

container.runScriptlet("Object::respond_to?(:processDoc, true)")

Is there an advantage to running JRuby if you don't know any Java?

I think you pretty much nailed it.

JRuby is just yet another Ruby execution engine, just like MRI, YARV, IronRuby, Rubinius, MacRuby, MagLev, SmallRuby, Ruby.NET, XRuby, RubyGoLightly, tinyrb, HotRuby, BlueRuby, Red Sun and all the others.

The main differences are:

  • portability: for example, YARV is only officially supported on x86 32 Bit Linux. It is not supported on OSX or Windows or 64 Bit Linux. Rubinius only works on Unix, not on Windows. JRuby OTOH runs everywhere: desktops, servers, phones, App Engine, you name it. It runs on the Oracle JDK, OpenJDK, IBM J9, Apple SoyLatte, RedHat IcedTea and Oracle JRockit JVMs (and probably a couple of others I forgot about) and also on the Dalvik VM. It runs on Windows, Linux, OSX, Solaris, several BSDs, other proprietary and open Unices, OpenVMS and several mainframe OSs, Android and Google App Engine. In fact, on Windows, JRuby passes more RubySpec tests than "Ruby" (meaning MRI or YARV) itself!

  • extensibility: Ruby programs running on JRuby can use any arbitrary Java library. Through JRuby-FFI, they can also use any arbitrary C library. And with the new C extension support in JRuby 1.6, they can even use a large subset of MRI and YARV C extensions, like Mongrel for example. (And note that "Java" or "C" library does not actually mean written in those languages, it only means with a Java or C API. They could be written in Scala or Clojure or C++ or Haskell.)

  • tooling: whenever someone writes a new tool for YARV or MRI (like e.g. memprof), it turns out that JRuby already had a tool 5 years ago which does the same thing, only better. The Java ecosystem has some of the best tools for "runtime behavior comprehension" (which is a term I just made up, by which I mean much more than just simple profiling, I mean tools for deeply understanding what exactly your program does at runtime, what its performance characteristics are, where the bottlenecks are, where the memory is going, and most importantly why all of that is happening) and visualization available on the market, and pretty much all of those work with JRuby, at least to some extent.

  • deployment: assuming that your target system already has a JVM installed, deploying a JRuby app (and I'm not just talking about Rails, I also mean desktop, mobile, other kinds of servers) is literally just copying one JAR (or WAR) and a double-click.

  • performance: JRuby has much higher startup overhead. In return you get much higher throughput. In practice, this means that deploying a Rails app to JRuby is a good idea, as is running your integration tests, but for developer unit tests and scripts, MRI, YARV or Rubinius are better choices. Note that many Rails developers simply develop and unit test on MRI and integration test and deploy on JRuby. There's no need to choose a single execution engine for everything.

  • concurrency: JRuby runs Ruby threads concurrently. This means two things: if your locking is correct, your program will run faster, and if your locking is incorrect, your program will break. (Unfortunately, neither MRI nor YARV nor Rubinius run threads concurrently, so there's still some broken multithreaded Ruby code out there that doesn't know it's broken, because obviously concurrency bugs can only show up if there's actual concurrency.)

  • platforms (this is somewhat related to portability): there are some amazing Java platforms out there, e.g. the Azul JCA with 768 GiBytes of RAM and 864 CPU cores specifically designed for memory-safe, pointer-safe, garbage-collected, object-oriented languages. Android. Google App Engine. All of those run JRuby.



Related Topics



Leave a reply



Submit