Using the Eclipse+rdt plug-in for Ruby development

Source: Internet
Author: User
Tags implement variables require return version variable window ruby on rails

   Brief IntroductionThis article describes how to use the Ruby development tool (RDT) plug-in in Eclipse for Ruby development. This article will benefit the Ruby developers who want to learn how to use the Eclipse infrastructure for Ruby Development, and the Java developers interested in Ruby development.

   One, why use Ruby?

Now, why are so many Java developers focused on Ruby? Ruby, a general-purpose scripting language created in Japan before 10, is a purely object-oriented language. Unlike Java technology, everything in Ruby is an object. Ruby's syntax is mainly derived from Smalltalk,python and Ada. Like the Java programming language, Ruby is a single inherited language, but it provides advanced features that are not implemented by Java technologies, such as closure (which can be considered an anonymous inner class) and mix-in (similar to interfaces but loosely bound to classes). Ruby is also highly portable and can run on all the mainstream operating systems.

Now, Ruby is very popular. People are starting to apply it to various types of applications. Because it's an interpretive type and uses dynamic typing, you can implement techniques that are difficult to implement in Java at run time. One of the surprising capabilities achieved by dynamically typed and expressive syntax is that it can create domain-specific languages (DSLs) with Ruby-they allow you to work at higher levels of abstraction, away from the most original language syntax. Ruby on Rails is a framework for creating Web applications based on database support, which also shows the power of Ruby. Rake, a version of Ruby's Make and Ant, is another proof of the powerful application of this language.

Another reason to use Ruby is that all programming enthusiasts are using it. Now, many Java gurus (such as Glenn Vanderburg,bruce Tate and Martin Fowler) are using Ruby. Even if you're not going to turn all your development into Ruby, the language is worth browsing for.

One of the main factors limiting the development of Ruby is that it lacks a good development environment (for those who do not want to learn Emacs). Today, RDT has changed all this, but it is best to experiment with eclipse.

   second, the installation

Before using Ruby, you must install the Ruby interpreter, the corresponding library, and the Ruby development environment.

   third, get Ruby

Ruby is available on all major platforms. In fact, your Linux or Cygwin distribution may already contain it. Please open a command prompt and enter "Ruby-v".
If you get a version number, then you are ready. Otherwise, you need to check the distribution package for the platform you choose.

If you are running Windows, it is easier to choose. RubyForge, as the ruby-specific equivalent of SourceForge, has a project called One-click Ruby Installer, which is used to build a ruby environment on the Windows platform. It also includes other tools, such as an IDE called freeride, but if you use Rdt, you can skip these during the installation process.

   Iv. Obtaining Documentation

Documentation and reference materials are critical when you start using a new language. You can get online references to ruby documents on the main Ruby site, but you'll find these documents a bit outdated (they're for Ruby 1.6 version, and the current version is 1.8.2). The problem is that the updated document is not currently translated from Japanese. However, you can find the latest Help document on the ruby-doc.org. It includes API-level documentation (the equivalent of Javadoc), as well as other tutorials and books.

If you're a serious Ruby developer, you should read "Programming Ruby:the pragmatic Programmer's Guide" (the author is Dave Thomas and Andy Hunt) for best programming results. This is a classic guide to Ruby and Ruby library programming. If you get this book, you can also refer to the book "Agile Development with Ruby on Rails" written by Dave Thomas. This is an introductory book about Ruby on Rails.

  V. Get RDT

Now that you've installed a work-friendly ruby with a good help document on your own computer, you'll need to install Rdt next. This is an Eclipse plug-in that provides many features that you might have been accustomed to when editing code. Rdt is a standard eclipse plug-in, so you can extract the compressed file directly into the Eclipse folder and handle everything else by the path in the document.

Now you are ready to create a Ruby project (see Figure 1).


Figure 1. Create a new Ruby project

Ruby is less demanding in terms of naming and directory structure than Java. Creating a project in Ruby actually creates only one directory and one project file (No. classpath file is required because Ruby has no classpath). Another notable difference from Java technology is that the Ruby Engineering Wizard does not create an explicit src and bin directory. Ruby is interpreted, so there is no output folder. If you have a small project, then you can put the Ruby source files in the same folder as the project files. As a choice, you can also create your own hierarchy of directories. As you can see, Ruby is less concerned about this than Java.

Next, you will create a Ruby source file. There is no specific wizard to create a Ruby source file. Ruby source files require less structure than Java technology, so in order to create a Ruby file, you need to right-click the project to create a new file (see Figure 2 below).


Figure 2. Create a Ruby source file

Don't forget to add the standard extension. RB to the file (it's a normal ruby extension). Creating a Ruby file should open the Ruby view as shown in Figure 3.


Figure 3. Creating a Ruby file will start the Ruby view

Ruby also provides a outline view (similar to the one provided by Java). Like its Java counterpart, it allows you to navigate between the elements of a Ruby source file. In Figure 4, the Raise_salary_by method is highlighted in both the outline and source view.


Figure 4. This outline view implements navigation in the source file

Like other complex plug-ins, Rdt also allows customization features to be added through the "Window>preferences" dialog box. See Figure 5 For this custom dialog box.


Figure 5. Rdt Install "Preferences" to enable customization

Parameter Selection "Preferences" menu item allows you to change the syntax highlights, formatting (the standard indent in Ruby World is two spaces, not four spaces, so be prepared for some adjustments), and so on. It also allows you to customize the editor template and select your own interpreter.

   VI. RDT Editor

In the Java world, we are accustomed to advanced editor features-which makes it more difficult to move to other environments that do not provide the same support. One of the features lacking in the Ruby IDE is the lack of a content assistant (it can provide context-sensitive identifier lookup capabilities). Fortunately, the Rdt Editor contains content assistants for Ruby code, as shown in Figure 6 below.


Figure 6. The Rdt Editor provides a content assistant

In Ruby, you can't assign a type to a variable or a method's return, and the identifier context determines the type at run time. Ruby introduces a method that is affectionately referred to as "duck typing". The idea of this approach is that if it accepts a false (quack) message, it must be a "duck". This may seem to hinder users who are accustomed to a strongly typed language, which can support some of the more powerful features of the Ruby language. For example, you can write an exception handler in Ruby-When you invoke a method that does not exist, and then the exception handler comes from parsing the method and then calling it. This metaprogramming level is difficult to implement in a strongly typed language.

One redeeming feature for content assistants is that Ruby uses specific naming conventions when using identifiers. For example, in Ruby, all member variables exist for the first time when they are used, and are all identified by an @ symbol as the first character of its name. If you use the Content Assistant to find a member variable, you can enter the @, so you can see only the member variables.


Figure 7. Naming conventions in the Ruby Help content Assistant

Dynamic typing still hinders context-sensitive issues in Ruby. In Figure 7, the only valid member variable is those that appear just above the method declaration, namely @name, @salary, and @hire_year. Some other members provided by the content assistant come from another class (defined later). Currently, the Rdt editor is not complex enough to filter all the correct syntax but the semantically incorrect entry.

  Vii. operation and Commissioning

One of the features of the IDE is to run and debug your built applications in the same environment. Rdt can enable both of these features.

  Eight, specify an interpreter

The implementation of this task is located in the Windows>preferences dialog box at the top of Ruby and the installed interpreters portal.
Ruby is an interpretive language, so you have to associate an interpreter with your environment before RDT can run or debug your application. This association is located at the "installed interpreters" entrance to the "Windows>preferences" dialog box at the top of Ruby.


Figure 8. To correlate a Ruby interpreter with the environment

For the ruby version you're using, you point the "Location" text field to the Bin directory, and then the RDT to pick up all the other information you need. Once you have associated the interpreter, you are ready to run the application.

   Ix. running the ruby application

Running a ruby application is actually similar to running a Java application. You can use the Run menu to create a run configuration, as shown in Figure 9 below.


Figure 9. Establish a run configuration in Rdt

When you run the application, RDT will switch control to the Ruby interpreter, and then run the application in the console window at the bottom of the Eclipse workspace, as shown in Figure 10 below.


Figure 10. Running a ruby application within Rdt

This example shows the same way that you run a console application, but run other types of applications, such as GUI programs.

   10, with Rdt debugging

One of the most critical features that an IDE requires to implement is the ability to effectively debug your application. The Ruby interpreter includes a command-line debugger, but who uses a command-line debugger in today's graphics tool age? Fortunately, the Ruby interpreter also broadcasts its debug information through a specific (configurable) port, and a tool like Rdt can listen to the port and provide the types of debugging support that developers expect.

To debug a ruby application in Rdt, we create a debug configuration like the run configuration created above. Then you can set a breakpoint by clicking the Groove on the left and use the debugger to start the application. As in Java technology, the IDE asks you if you want to switch to the debug view. If you want, you'll see something like Figure 11 below:


Figure 11. Debugging Ruby Applications in Rdt

The same debugging level as Java technology debugging is supported in Rdt. The top left grid shows the currently executing thread, and the upper-right grid shows the value of the variable. As in the Java programming language, you can further analyze objects to see their intrinsic member variable values. The middle-left grid shows the source code for the currently running application, and the grid on the right in the middle shows the outline view-it acts as an editor here, allowing you to navigate by clicking an identifier. The bottom of the debug window shows the debugging information that the Ruby interpreter broadcasts on port 1098, while the Rdt listens to debugging information on that port.

Debugger support is a feature of Rdt. Even if you have an editor with excellent Ruby support, you still have to rely on the command-line debugger to debug your application. Having a fully functional debugger will greatly improve the productivity of your software.

  XI. Testing

One of the most difficult Ruby features to implement as a Java developer is dynamic typing. If you are accustomed to a strongly typed language, dynamic typing may seem a bit confusing. DT allows a variety of advanced metaprogramming techniques-which are difficult or impossible to implement in a strongly typed language. Of course, you can discard the security settings for compile-time type checking. So is there a way to achieve the best combination of the two worlds?

Unit tests should be implemented for every language, but they are particularly critical in dynamic languages. Unit tests reveal more content than compilation. In fact, you should change the point of view on the relationship between unit testing and compilation. Recently, a senior software development expert said, "in the next five years, we will be compiled as a weak implementation of unit testing." Unit tests can provide validation (verification)-coding will be implemented as scheduled, rather than just checking the grammar of what you enter.

Given the extreme importance of unit testing in the Ruby world, you'll want Rdt to make unit tests easy to run. That's true! Unit tests are included in Ruby, so you don't have to download any other extensions. A TestCase class and a testsuite concept are included in the Ruby Library. You can create your unit tests just as you would any other Ruby class, from the Test::unit::testcase neutron class to your test. Listing 1 is an instance of an employee class.

Listing 1. Employee class

Class Employee
Def initialize (name, salary, Hire_year)
@name = Name
@salary = Salary
@hire_year = Hire_year
End
Attr_reader:name,: salary,: Hire_year
def raise_salary_by (perc)
@salary + + (@salary * (perc * 0.01))
End
def to_s
' Name is #{@name} ', Salary is #{@salary}, ' + ' hire year is #{@hire_year} '
End
End

The corresponding unit tests are as follows:

List 2. Unit tests corresponding to the employee class

Require ' test/unit/testcase '
Require ' Test/unit/autorunner '
Require ' HR '
Class Testemployee < Test::unit::testcase
@ @Test_Salary = 2500
DEF setup
@emp = Employee.new ("Homer", @ @Test_Salary, 2003)
End
def test_raise_salary
@emp. Raise_salary_by (10)
expected = (@ @Test_Salary * 0.10) + @ @Test_Salary
ASSERT (expected = = @emp. Salary)
End
End

To run this unit test, you can create a run configuration for the unit test class-as a test::unit type, as shown in Figure 12.


Figure 12. Rdt includes a test::unit run configuration

When you run this test, you get the same support elements as the Java unit test, including the class JUnit grid in the lower-left corner.


Figure 13 Example of a unit test running in the IDE

You can also create testsuites in Ruby. Testsuites is a Ruby class that defines a suite method that returns Testsuite. This testsuite consists of the suite automatically defined in each testcases. Listing 3 is an example Testsuite for several classes.

List 3. Examples of some classes Testsuite

Require ' test/unit/testsuite '
Require ' Test/unit/ui/tk/testrunner '
Require ' Test/unit/ui/console/testrunner '
Require ' Testemployee '
Require ' Testmanager '
Class Testsuite_alltests
def Self.suite
Suite = Test::unit::testsuite.new ("HR Tests")
Suite << Testemployee.suite
Suite << Testmanager.suite
Return Suite
End
End
#Test:: Unit::ui::tk::testrunner.run (testsuite_alltests)
Test::unit::ui::console::testrunner.run (testsuite_alltests)

Unlike the previous instance of running a single testcase, the suite here runs as a stand-alone application. Ruby has two methods to display the results of a testsuite. The first is "console Test Runner"-it outputs its results on the console. The second is "Tk Testrunner", which creates a familiar dialog box to display the results of the test. Figure 14 shows a TK testsuite dialog box.


Figure 14. Testsuite dialog box in figure form

   12, the new version of the improvement

The current version of Rdt is 0.50. Currently, its developers are developing version 0.60. The areas to be improved in the next release include:

· Code folding feature-provides code folding for classes and methods.

· Outline view-Provides more detailed information, including support for local variables.

· RI View-use Ruby's RI tool from a Rdt view.

· Task label-Creates a task in Ruby notation as a configurable keyword, such as todo,fixme.

· Editor improvements-Automatically add curly braces, parentheses, and single/double quotes; and better code assistants.

· Inspector Shortcuts-provides configurable shortcuts for frequently used inspectors during a debugging session, such as all methods that display an object, global constants, and so on.

The next version will better use the JRuby bytecode compiler. JRuby is a project that allows Ruby code to be compiled into Java bytecode. This means that later versions of RDT will be more easily added to the eclipse world, even providing better support.

   13, Summary

Ruby is ultimately widely accepted and applied by the industry. This is partly due to the successful drive of the Ruby on Rails project. Now it's time to consider how to add Ruby to your "competition toolbox". Of course, Rdt is also an important factor in attracting your participation in this area.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.