Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is JRuby?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is JRuby". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is JRuby".

What is JRuby?

As a Java programmer, you don't want to use Ruby in a product until you can interact with existing Java applications and class libraries that support many kinds of basic functions of Ruby. So what is JRuby? An open source Ruby interpreter under JRuby,JVM that can use the Ruby class library in Java. Just like the standard Ruby interpreter, Ruby code can be executed correctly in JRuby except for calling native methods (C code) or Java class libraries using Ruby.

Compared to the common language runtime of Microsoft's. Net platform, JVM tends to support only one language. But in fact, the JVM platform can support not only Java, but also Python, JavaScript, Groovy, Scheme, and various other languages, which means that Ruby code can interact well with these languages when necessary.

The development of JRuby

Now that we understand what JRuby is, let's take a look at how JRuby has evolved. In mid-July 2006, JRuby had only one preview version (0. 9). But it has grown rapidly: a team of volunteers has released a total of five versions since January 2005. JRuby has matured through continuous evaluation testing of the standard interpreter, and now more than 90% of the tests are basically supporting the Ruby on Rails framework.

To try JRuby, make sure that Java SE 5 is installed and that the JAVA_HOME environment variable is set. Download the zip file from the JRuby project page and unzip it. Set the JRUBY_HOME environment variable to the root directory of the JRuby installation. You can try to interact with jirb in the bin directory. In most cases, you will use the JRuby interpreter-create a file that passes the file name as a parameter to the batch script in JRuby's bin directory.

The function of JRuby

If you know what JRuby is, what is its function? In addition to executing the previous Ruby code, you can still use JRuby to construct the Java object, call the Java method, and inherit from a Java class. A Ruby class can implement the Java interface-- you can call the Ruby method statically in Java if necessary.

In order to access Java from Ruby, you need to initialize the class library, starting with the "java" command. Next, use the include_class method to specify the Java class you want to use, for example, include_class "javax.jms.Session". You can use include_package to import the entire Java package into the Ruby module. Just like the wildcard statements of the Java import package, it is wise to avoid name conflicts caused by the use of include_package; in JRuby, it is particularly unwise for the interpreter to search all packages for the required classes. Use include_class as strictly as possible.

Many Java standard classes have the same name as the Ruby class. To resolve such conflicts, pass a block of code to the include_class function, returning a new name for the Java class, and JRuby will use that name as an alias for the Java class. (see Listing4)

Listing 4. Include a Java class with clashing name

Require "java" # The next line exposes Java's String as JStringinclude_class ("java.lang.String") {| pkg, name | "J" + name} s = JString.new ("f")

Alternatively, you can create a Ruby module that contains the definition of the java class, but needs to be in an isolated namespace. For example:

Listing 5. Java module importing multiple Java classes

Require "java" module JavaLang include_package "java.lang" ends = JavaLang::String.new ("a")

Thank you for your reading, the above is the content of "what is JRuby", after the study of this article, I believe you have a deeper understanding of what JRuby is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report