In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the basic knowledge points of JRuby". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the basic knowledge points of JRuby"?
Introduction to JRuby
Ruby is a language that is under heated discussion in the programming world. The language is powerful and easy to use; Ruby is also tightly bound to web application development frameworks such as Ruby on Rails. On the other hand, in the past decade, Java is rapidly growing into the industry's leading object-oriented software development language and running on many platforms. JRuby is a 100% pure Java implementation of the Ruby programming language. This article is an introduction to JRuby; at the same time, it will show you how it combines the advantages of the Java language to provide Java developers with a fast and powerful option for creating applications.
II. Scripted language of Ruby- sweeping the computing world
In recent months, almost everyone in the programming field has probably been exposed to Ruby. The increasingly popular Ruby on Rails web development framework is helping to make Ruby a language for rapid development and testing of applications. As an interpretive scripting language, Ruby provides fast and easy object-oriented programming techniques, as well as many very clean features such as closure, blocks, and mixins.
In addition, Ruby is highly portable and can be widely run on Unix/Linux,Windows and MacOS platforms. For a comprehensive understanding of Ruby, I suggest you read W.Jason Gilmore's article.
III. Ten years of Java- 's brilliant object-oriented language
Java was first founded by James Gosling of Sun Microsystems in the 1990s. At that time, the main purpose of Gosling's development of the language was to provide a virtual machine and a C-style markup language, while making it more consistent and simpler than CAccord. Another major goal of Java is to provide an ideal language that "write once and run anywhere" (WORA), allowing a program to be developed on one platform and run on another without any recompilation. The Java platform consists of a Java runtime environment and a Java software development kit (SDK). Today, Java can provide SDK for platforms ranging from mobile devices to enterprise systems, corresponding to its JavaSE,EE and ME technologies, respectively. In the past decade, Java has been widely used in many development fields, such as mobile devices, unattended systems, web applications, financial systems, real-time systems and so on.
4. Ruby running on JRuby-Java virtual machine (JVM)
JRuby is a 100% pure Java implementation of the Ruby programming language, which is distributed under three open source licenses, CPL,GPL and LGPL. It is a 1.8.4 Ruby interpreter that provides most of the built-in classes for Ruby. JRuby supports defining and interacting with Java classes from a Ruby program, as well as implementing the Bean scripting framework. At the time of this writing, the current version of JRuby is 0.9.1. JRuby enables Ruby programs to access Java classes, allowing them to be used as first-level objects within the program. Today, the founders of JRuby, Thomas Enebo and Charles Nutter, have been hired by Sun to specialize in research and development of JRuby (51CTO Editor's Note: now Sun will be acquired by Oracle, while the core staff of JRuby feel uncertain about the future of Sun and have moved to Engine Yard, a Ruby on Rails cloud hosting company, including Thomas Enebo and Charles Nutter. But it might be a good momentum for JRuby.
Introduction to JRuby: run your JRuby programs
The release of JRuby comes with a tar.gz file. In this article, I am based on the Windows system and use the jruby-bin-0.9.1.tar.gz binary distribution of JRuby and the Java SE version 1.5.0x06. After unzipping these release files, a jruby-0.9.1 folder appears with subfolders: bin,docs,lib and samples. Note that you can add a JRUBY_HOME environment variable that points to this directory, and then add% JRUBY_HOME%bin ($JRUBY_HOMEbin on UNIX) to your system path. The bin directory of the JRuby distribution contains a jruby.bat file that is used to run the JRuby interpreter program. Run "jruby-version" from the command line to test that JRuby is working correctly:
C:JRubyjruby-0.9.1 > jruby- version ruby 1.8.5 (0) [java]
In the samples directory above, you can find some sample Ruby programs that demonstrate the functionality of JRuby. As an example, you can take a look at the following java2.rb example. This example contains the following Ruby code:
Require "java" filename = ". / samples/java2.rb" fr = java.io.FileReader.new filename br = java.io.BufferedReader.new fr s = br.readLine print "-", filename, "- n" while s puts s.to_s s = br.readLine end print "-", filename, "end-n"; br.close
The * line of code is responsible for starting Java support for JRuby and allowing a Ruby program to use the Java class.
The program defines a java.io.FileReader that points to the file ". / samples/java2.rb". It then declares a java.io.BufferedReader for buffering data read through FileReader. This program reads all the code from this file and outputs it to the console. Here is the output of running the Ruby script using JRuby:
C:JRubyjruby-0.9.1 > jruby .samplesjava 2.rb -. / samples/java2.rb- require "java" filename = ". / samples/java2.rb" fr = java.io.FileReader.new filename br = java.io.BufferedReader.new fr s = br.readLine print "-", filename, "- n" while s puts s.to_s s = br.readLine end print "-", filename "end-n" Br.close -. / samples/java2.rb end-C:JRubyjruby-0.9.1 >
So far, you have been able to run a Ruby script using JRuby; the script can use the Java class to read a file and output its contents. This is a powerful concept that JRuby presents to Ruby users.
Using Java classes in JRuby (including naming conflicts)
Now that you are able to run an example, let's take a look at how JRuby allows the use of Java classes in Ruby programs. The most important method that JRuby provides is the Kernel#include_class method. This method allows Ruby to use the Java class as follows:
Require "java" # Include Java's FileReader include_class "java.io.FileReader" filename = ". / samples/java2.rb" fr = FileReader.new filename
However, there may be naming conflicts between the Java class and the Ruby class. The use of strings is an example; java.util.String is provided in Java and Kernel::String is provided in Ruby. To resolve this naming conflict, you can rename the class when you include it-by passing a block of code to the include_class method call:
Require "java" # Include Java's String as JString include_class ("java.lang.String") {| package, name | "J" + name} s = JString.new ("Hello World from JRuby!") Puts s
You can also avoid this naming conflict by defining a Ruby module that includes a Java class definition:
Require "java" module JavaLang include_package "java.lang" end s = JavaLang::String.new ("Hello World from JRuby!") Puts s
A sample program, javaHello.rb, is provided below, which uses Java's HashMap to store three strings and print them. What's interesting about this program is that it calls the each method in Ruby's java.util.Set (returned by calling the java.util.HashMap.keySet () method). This allows the result key returned by the "each" method to be passed to the corresponding code block, which then uses the key to get the corresponding string and output the string.
Require "java" module JavaLang include_package "java.lang" end include_class ("java.util.HashMap") {| pkg, name | "J" + name} s = JavaLang::StringBuffer.new ("Hello Java World") puts s.append (", I love JRuby") m = JHashMap.new () m.put ("java", "Java") m.put ("ruby", "Ruby") m.put ("jruby" "JRuby" puts m m.keySet () .each {| key | puts m.get (key)}
Here is the output when you run it using JRuby:
C:\ JRuby > jruby javaHello.rb Hello Java World, I love JRuby {java=Java, ruby=Ruby, jruby=JRuby} Java Ruby JRuby C:\ JRuby >
A program written entirely in Java, similar to the above, looks like this:
StringBuffer s = new StringBuffer ("Hello Java World"); s.append (", I love JRuby"); HashMap m = new HashMap (); m.put ("java", "Java") m.put ("ruby", "Ruby") m.put ("jruby", "JRuby") for (String key: m.keySet ()) {println (m.get (key);}
7. Use Swing in JRuby
Swing is a GUI framework that allows developers to create graphical applications using the Java language. Here is an example program, javaSwingHello.rb, which uses Java's Swing to print a message:
Require 'java' include_class "javax.swing.JFrame" include_class "javax.swing.JLabel" frame = JFrame.new () jlabel = JLabel.new ("Hello from JRuby with Swing") frame.getContentPane (). Add (jlabel) # frame.content_pane.add (label) frame.pack () frame.setVisible (true) frame.visible = true
Run this program from the command line as follows:
C:\ JRuby > jruby javaSwingHello.rb
A new window with the text "Hello from JRuby with Swing" appears:
At this point, I believe you have a deeper understanding of "what are the basic knowledge points of JRuby?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.