How to Use the ruby development tool (RDT) Plug-in eclipse for Ruby Development

Source: Internet
Author: User
Tags create domain
1. Why Ruby?

Why do many Java developers focus on Ruby? Ruby is a general-purpose scripting language created in Japan 10 years ago. It is a pure object-oriented language. Unlike Java, everything in ruby is an object. Ruby syntax mainly comes from smalltalk, Python and Ada. Like the Java programming language, Ruby is a single-inherited language, but it provides some advanced features that are not implemented by Java technology, for example, closed (can be considered an anonymous internal class) and mix-in (similar to the interface, but loosely bound to the class ). Ruby is also highly portable and can run on all mainstream operating systems.

Ruby is currently very popular. People are starting to apply it to various types of applications. Because it is interpreted and dynamically typed, You can implement the skills that are hard to implement in Java at runtime. One of the surprising capabilities implemented by dynamic typing and expressive syntaxes is that it can use Ruby to create domain-specific languages (DSL) -They allow you to work on a higher level of abstraction layer to stay away from the original language syntax. Ruby on Rails is a framework for creating database-based Web applications. This shows Ruby's strength. Rake, a ruby version that combines make and ant, is another proof of powerful application of this language.

Another reason for using Ruby is that all programming enthusiasts are using it. Currently, many Java experts (such as Glenn vandreburg, Bruce Tate, and Martin Fowler) are using Ruby. Even if you are not planning to turn all your development into Ruby, this language is worth browsing.

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

  Ii. Installation

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

  3. Obtain Ruby

Ruby can be used on all major platforms. In fact, your Linux or cygwin distribution package may already contain it. Open a command prompt and enter "Ruby-V ".
If you get a version number, you are ready. Otherwise, you need to check the distribution package of the platform you selected.

If you are running Windows, it is easier to choose. Rubyforge, as a ruby-specific equivalent on SourceForge, has a project called one-click Ruby installer, which is used to build a ruby environment on a Windows platform. It also includes other tools, such as an IDE called freeride. However, if you use RDT, you can skip these tools during installation.

  Iv. Document Retrieval

When you start using a new language, documents and references are crucial. You can get online reference materials for Ruby documents on the main Ruby website, but you will find that these documents are outdated (they are for Ruby 1.6, and the current version is 1.8.2 ). The problem is that the updated documents have not yet been translated from Japanese. However, you can find the latest help documentation on the Ruby-doc.org. It includes API-level documents (equivalent of javadoc) and other tutorials and books.

If you are a serious Ruby developer, read programming RUBY: the pragmatic Programmer/'s Guide by Dave Thomas and Andy hunt) this book gets the best programming results. This is a classic guide to Ruby and Ruby library programming. If you get this book, you can also refer to agile development with Ruby on Rails written by Dave Thomas. This is an entry-level book on Ruby on Rails.

 5. Obtain RDT

Now that you have installed a working Ruby with good help documentation on your computer, you need to install RDT. This is an Eclipse plug-in that provides many features you may have become accustomed to when editing code. RDT is a standard Eclipse plug-in. Therefore, you can directly extract the compressed file to the eclipse folder and process everything else using the path in the document.

Now you are ready to create a ruby project (see figure 1 ).


Figure 1. Create a New Ruby Project

Compared with Java, Ruby does not have strict requirements on naming and directory structure. Creating a project in Ruby actually creates only one directory and one project file (The. classpath file is not required because Ruby does not have classpath ). Another notable difference from Java technology is that the ruby engineering wizard does not create explicit SRC and bin directories. Ruby is interpreted, so there is no Output Folder. If you have a small project, you can put the ruby source file in the same folder as the project file. You can also create your own directory hierarchy. You will find that Ruby seldom cares about this compared with Java.

Next, you will create a ruby source file. No specific wizard is used to create a ruby source file. Compared with Java technology, Ruby source files only require a small amount of structure. Therefore, to create a ruby file, you need to right-click the project to create a new file (see figure 2 ).


Figure 2. Create a ruby source file

Do not forget to add the standard extension. RB to this file (it is a normal Ruby extension ). Creating a ruby file should open the ruby View Shown in 3.


Figure 3. Creating a ruby file will start the ruby View

Ruby also provides an outline view (similar to the one provided by Java ). Just like its Java counterparts, it allows you to navigate between elements in the ruby source file. In Figure 4, The raise_salary_by method is highlighted in both the outline and source code views.


Figure 4. This Outline View enables navigation in the source file

Like other complex plug-ins, RDT also allows you to add custom features through the "WINDOW> Preferences" dialog box. The custom dialog box is shown in Figure 5.


Figure 5. RDT installation "Preferences" for customization

Select the "Preferences" menu item for the parameter to allow you to highlight and format the syntax (Standard indentation in the ruby world is two spaces rather than four spaces, so be prepared to make some adjustments. It also allows you to customize the editor template and select your own interpreter.

  Vi. RDT Editor

In the Java World, we are used to advanced editor features-which makes it difficult to switch to other environments that do not provide the same support. One of the features lacking in Ruby IDE is that it does not provide content assistants (it can provide context-related identifier lookup functions ). Fortunately, the RDT Editor contains content assistants for Ruby code. See figure 6.


Figure 6. RDT Editor provides content Assistant

In Ruby, you cannot assign a type to a variable or method, but the identifier context determines the type at runtime. Ruby introduces a method called "duck typing" affectionately. The idea of this method is that if it accepts a quack message, it must be a "Duck ". This seems to impede users who are used to strongly typed languages. This loose type coupling can support some of the more powerful features of Ruby. For example, you can write an exception processor in ruby-activate it when you call a method that does not exist, and then use the Exception Processing processor to freely analyze the method and then call it. This meta-programming level is hard to implement in a strongly typed language.

One Recovery feature of content assistants is that Ruby uses a specific naming convention when using identifiers. For example, in ruby, all member variables exist when they are used for the first time and are all identified by a @ symbol as the first character of their names. If you use content assistant to search for member variables, you can enter @, so that you can only see member variables.


Figure 7. Naming Conventions in Ruby help content Assistant

Dynamic typing still hinders context-sensitive issues in ruby. In Figure 7, the only valid member variables appear on the method Declaration, that is, @ name, @ salary, and @ hire_year. Some other members provided by the content assistant are from another class (defined later ). Currently, the RDT editor is not complex enough to filter all entries with correct syntax but incorrect semantics.

  VII. Operation and debugging

One of the features of IDE is to run and debug your applications in the same environment. RDT can enable these two functions.

  8. Specify an interpreter

The implementation of this task lies in the upper part of Ruby and the "Windows> Preferences" dialog box at the "installed interpreters" entry.
Ruby is an explanatory language. Therefore, before RDT can run or debug your application, you must associate an interpreter with your environment. This association is located at the "installed interpreters" entrance in the "Windows> Preferences" dialog box at the top of Ruby.


Figure 8. associate a Ruby interpreter with the Environment

For the ruby version you are using, you need to point the "location" text field to the bin directory, and then RDT will select all other required information. Once you associate the interpreter, you are ready to run the application.

  9. Run 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. See figure 9.


Figure 9. Create a run configuration in RDT

When you run the application, RDT switches the control to the Ruby interpreter and runs the application in the console window at the bottom of the eclipse work zone. For details, see 10.


Figure 10. Run a ruby application in RDT

This instance shows how to run a console application, but run other types of applications (such as GUI programs) in the same way.

  10. Use RDT for debugging

One of the most critical features that an IDE requires to implement is the ability to effectively debug your applications. The Ruby interpreter includes a command line debugger. But in today's graphic tools age, who will use a command line debugger? Fortunately, the Ruby interpreter broadcasts debugging information through a specific (configurable) port, a tool like RDT can listen to the port and provide debugging support types that developers expect.

To debug a ruby application in RDT, we need to create a debug configuration like the run configuration created above. Then, you can click the left groove to set a breakpoint and use the debugger to start the application. Just like in Java technology, IDE will ask you if you want to switch to the debugging view. If you want to, you will see content similar to the content shown in Figure 11 below:


Figure 11. debugging Ruby applications in RDT

RDT supports debugging at the same level as Java. The grid at the top left shows the currently executed thread, and the grid at the top right shows the variable value. Just like in Java programming languages, you can further analyze objects to view their inner member variable values. The grid on the left in the middle shows the source code of the currently running application, and the grid on the right in the middle shows the Outline View-it serves as the editor here, allows you to navigate by clicking an identifier. The debugging information broadcasted by the Ruby interpreter on port 1098 is displayed at the bottom of the debug window, And the RDT listens to the debugging information on the 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 production efficiency of the software.

11. Test

Dynamic typing is one of the most difficult Ruby features for Java developers. If you are used to a strongly typed language, dynamic typing seems a bit confusing. DT allows a variety of advanced metaprogramming techniques-this is difficult or impossible to implement in a strongly typed language. Of course, you can discard the security settings of the type check at the time of compilation. So is there a way to achieve the best combination of the two worlds?

Unit testing must be implemented in every language, but it is particularly critical in dynamic languages. Unit Testing reveals more content than compilation. In fact, you should change your point of view on the relationship between unit testing and compilation. Recently, a senior software development expert believes that "in the next five years, we will use compilation as a weak implementation form of unit testing ". Unit testing provides verification-encoding will be implemented on schedule, rather than just checking the syntax of your input content.

Considering the extreme importance of unit testing in the ruby world, you certainly want RDT to make unit testing easy to run. Indeed! Unit Tests are included in ruby, so you don't have to download any other extensions. The ruby library contains a testcase class and a testsuite concept. You can create your unit test like creating any other Ruby class, subclass your test from test: Unit: testcase. List 1 is an instance of the employee class.

List 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 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 will get the same support elements as the Java unit test, including the class JUnit lattice in the lower left corner.


Figure 13. Example of a unit test running in 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 a suite automatically defined in each testcases. List 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 example of running a single testcase, the suite program runs as an independent application. Ruby has two methods to display the results of 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 test results. Figure 14 shows a TK testsuite dialog box.


Figure 14. graphical testsuite dialog box

  12. Improvements in the new version

The current version of RDT is 0.50. At present, its developers are developing Version 0.60. The following improvements will be made in the next version:

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

· Outline View-provides more detailed information, including support for local variables.

· Ri view-use Ruby Ri tools from an RDT view.

· Task tag-create a task with configurable keywords (such as todo and fixme) in the form of Ruby annotations.

· Editor improvements-automatically add braces, Parentheses, and single/double quotation marks, as well as better code assistants.

· Inspector shortcut-Provides configurable shortcuts for frequently used inspector during a debugging session, such as displaying all methods and global constants of an object.

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

   Summary

Ruby is widely recognized and applied by the industry. This is partly due to the success of the Ruby on Rails project. Now, it's time to consider how to add Ruby to your competing toolbox. Of course, RDT is also an important factor that attracts you to join this field.

This article from: http://blog.csdn.net/huangxiaoke2000/archive/2006/08/30/1145814.aspx

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.