Ruby development with Eclipse

Source: Internet
Author: User
Use Eclipse for Ruby development easily-Linux general technology-Linux programming and kernel information. The following is a detailed description. [Indent] About three years ago, developerWorks released the "Ruby Development Tools plug-in using Eclipse", which introduced some features provided in the Ruby Development Tools (RDT) Plug-in of Eclipse, it was V0.5 at that time. In this article, we will review this tool. Now, RDT is called Aptana RadRails and can be obtained as a plug-in of Aptana Studio or Eclipse. This article introduces some new features of the plug-in. [/Indent] the name and location of the Ruby Development Tools (RDT) Plug-In have changed, but the basic functions and license terms remain unchanged in nature. If you are not familiar with the concept of developing Ruby applications on Eclipse, read the article titled "using Eclipse Ruby Development Tools plug-in" written by Neal Ford first, learn how to create Ruby files, customize the Ruby Editor, set the debugger, run Ruby from Eclipse, and test from Eclipse. In this article, we will base on Ford's article and view it:
  • Code folding-you can enable folding for classes and methods.
  • Outline View-view the Outline View in more detail and learn how to use it.
  • RI view-use Ruby's Interactive document utility from RDT view.
  • Task tag-create a task for configurable keywords (such as TODO and FIXME) in Ruby annotations.
  • New features in the Ruby browser-RDT allow you to easily view and browse all Ruby resources available in the project.
  • Editor improvements-Automatically complete square brackets, Parentheses, and single/double quotation marks; and better code HELP features.
Getting started
RDT is now called Aptana RadRails V1.0.3 and can be obtained at Aptana.com. To download a new version or update the Eclipse plug-in, follow the RadRails instructions in Aptana. Eclipse users should be familiar with these instructions. Note: If you have not installed the Aptana plug-in the Eclipse workspace before, you should follow the instructions for inserting Aptana into the existing Eclipse configuration Aptana. During installation, you also need to refer to the Aptana RadRails document to learn about how to solve OS-specific problems.

[Table = 98%] [tr] [td]

(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.linuxeden.com/upimg/allianz 080916/1547390.gif'); ">
[Img = 8, 6] average

Code folding
Almost all Integrated Development environments (IDE) such as Eclipse support the concept of code folding. Code folding allows you to collapse the source code of other parts when processing some content in the source code file. This will allow you to manage a large amount of code in a window, without having to ignore this. To enable or disable code folding in RadRails, make sure that it is in the Ruby perspective and select Window> Preferences. The Preferences window should pop up. Scroll down to the left pane until you find the Ruby plug-in option and expand it with Ruby> Editor> Folding. You should see content similar to figure 1.

Figure 1. Eclipse window preferences allow you to customize the interface appearance and feature set of the Eclipse perspective
[Img = 484,343] http://www.linuxeden.com/upimg/alli%080916/1547392.jpg%/img]
Make sure that Enable folding when opening a new editor is selected and OK is selected.
In "using the Ruby Development Tools plug-in of Eclipse", we created a Ruby project. If you have not done so, create a project. If you do not want to refer to this article, listing 1 shows the relevant code. Create a Ruby file consisting of the code shown in Listing 1. You also need a Ruby class named hr. rb.

List 1. hr. rb
[Table = 98%] [tr] [td] class Employee def initialize (name, salary, hire_year) @ name = name @ salary = salary @ hire_year = hire_yearend def to_s "Name is # (@ name), salary is # (@ salary ), "+" hire year is # (@ hire_year) "end def raise_salary_by (perc) @ salary + = (@ salary * 0.10) endendclass Manager <Employee def initialize (name, salary, hire_year, asst) super (name, salary, hire_year) @ asst = asstend def to_s super + ", \ tAssistan info: # {@ asst}" end def raise_salary_by (perc) perc ++ = 2007-@ hire_year super (perc) endend [/td] [/tr] [/table]
When viewing this class in Eclipse, you should see a series of minus signs similar to Figure 2. Each of these minus signs represents a folding point. Select a minus sign to collapse the code at this point. Try to select the collapse points of all methods in the hr. rb Employee class.

Figure 2. RDT supports code folding in Ruby
[Img = 491,570] http://www.linuxeden.com/upimg/alli%080916/1547393.jpg%/img]


If you select a collapse point for all the methods of the Employee class, you will see content similar to figure 3.

Figure 3. collapse the methods in the Employee class
[Img = 482,259] http://www.linuxeden.com/upimg/alli%080916/1547394.jpg%/img]


As you can see, code folding allows you to manage and control the work environment, allowing you to focus on the code you are interested in. If you return to the Preferences screen and view the Ruby code folding settings, you can also note that you can use RDT to fold comments, methods, or internal classes when opening the file at first. Many people find that this is a way to quickly view the basic structure of the class, without being distracted by a lot of code. However, as you will see in the next section, there is a simpler way to get a code overview.

[Table = 98%] [tr] [td]

(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.linuxeden.com/upimg/allianz 080916/1547390.gif'); ">
[Img = 8, 6] average


Outline View
In "using the Eclipse Ruby Development Tools plug-in", we mentioned that outline view is a way to browse the elements of Ruby source files. The outline view also allows you to quickly view tree representations of classes, internal classes, methods, and variables. If you do not see the Outline View in the Ruby perspective for some reason, you can go to Window> Show View> Outline to easily open it, which will open the dockable code) outline View. For the hr. rb file, when the outline is fully expanded, you should see content similar to Figure 4.

Figure 4. outline view shows the expanded Tree Representation of the hr. rb File
[Img = 266,288] http://www.linuxeden.com/upimg/allianz 080916/1547397.jpg#/img]


Each symbol and symbol color has a specific meaning that is either obvious or not obvious. If this is not the case, go to Window> Preferences and select Ruby> Appearance> Members Sort Order to easily find information about these meanings, which provides clues to the Outline View, and allows you to adjust and arrange the representation order of the Outline View symbols. You should see content similar to the one shown in Figure 5.

Figure 5. Member sorting order
[Img = 425,414] http://www.linuxeden.com/upimg/alli%080916/1547398.jpg%/img]


The Code outline view is a powerful tool that helps you understand how to construct code. But what should you do when you forget the purpose of a function or class? In the next section, you will learn how to access the Ruby documentation from Eclipse.

[Table = 98%] [tr] [td]

(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.linuxeden.com/upimg/allianz 080916/1547390.gif'); ">
[Img = 8, 6] average


Ruby Interactive view
Unless there is a large amount of memory, it is easy to forget some classes, methods, and modules provided by Ruby. Ruby provides documents and Ruby Interactive (RI), which is an Interactive document reader that allows you to quickly search for and find information in Ruby documents. To use RI in combination with Eclipse, go to Window> Preferences and select Ruby> RI/rdoc to configure the RadRails plug-in to access it. Here, you will see two fields: RDoc path and Ri path. Each field is expected to obtain the complete path of the rdoc and ri scripts. If Ruby installation is in drive C, it will be similar to C: \ ruby \ bin \ rdoc and C: \ ruby \ bin \ ri content, as shown below (Mac OS X and Linux®You need to adjust the path accordingly ). Enter the correct path and select OK.

Figure 6. Set the Ruby Interactive document reader
[Img = 479,325] http://www.linuxeden.com/upimg/alli%080916/15473911.jpg%/img]


After you complete this operation, you should be able to see the RI tab in the lower half of the Eclipse Ruby perspective. If you do not see this tab for some reason, go to Window> Show View and select RI, select this tab, and type Array in the input field. RI will try to find matching search content, or simply scroll through available options and click an entry. For Array, you should see content similar to figure 7.

Figure 7. query the sample output of the Array class in RI
[Img = 461,407] http://www.linuxeden.com/upimg/alli%080916/15473912.jpg%/img]


RI view is very useful, it allows you to focus on and stay in the development environment, without switching to a Web browser or other applications to view the document. However, sometimes you may encounter problems that cannot be solved at the time or you do not have time to handle. Developers often leave comments in the code, but these comments are easy to forget. In the next section, we will see how to use the task tag to mark the entries to continue processing.

[Table = 98%] [tr] [td]

(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.linuxeden.com/upimg/allianz 080916/1547390.gif'); ">
[Img = 8, 6] average


Task tag
One of the most useful views of Eclipse and RadRails plug-ins is the Tasks view. You can use the Tasks view as your own or project-based to-do list, which allows you to mark code with entries such as ToDo, FixMe, and Optimize. You can configure your own Task tag by going to Window> Preferences, and then to Ruby> Task> Tags. You should see content similar to figure 8.

Figure 8. Task tag area, which allows you to create a new custom tag for marking code
[Img = 572,257] http://www.linuxeden.com/upimg/alli%080916/15473915.jpg%/img]


You can add a tag to the Code, for example, # ToDo: Implement give_bonus method.
Try to add the comment to the hr. rb file of the Employee class and save it. Select the Task pane in the lower half of the Eclipse View, or if you do not see it for some reason, go to Window> Show View> Task, then you will see content similar to that shown in Listing 2.

List 2. view the task
[Table = 98%] [tr] [td] Completed Priority Description Resource Path Location Creation Time TODO: Implement give_bonus method hr. rb RubyTest line 7 1173676801984 [/td] [/tr] [/table]
It is very useful to define your own task tags in a team development environment. The task tag allows the team to define specific types of tasks related to the tag, which will be viewed by other developers, even if they do not view the specific code that contains the task. This will allow the team to use the Code itself for communication, rather than using emails or other communication methods that are easy to lose. In the next section, we will view another perspective that allows you to quickly browse projects and find information.

[Table = 98%] [tr] [td]

(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.linuxeden.com/upimg/allianz 080916/1547390.gif'); ">
[Img = 8, 6] average


Ruby Browser
The RadRails plug-in this release has many new features, including the Ruby Browsing view. To obtain this view, you only need to go to Window> Open Perspective> Ruby Browsing. You should see content similar to figure 9.
This perspective contains multiple views of the Ruby element. This project displays all projects in the workspace. In the Types view, you will see all the classes and Types in the project, and in the Members view, you will see the specific elements of the class selected from the Types view. Double-click the entry to enable the RadRails plug-in to open the file related to the element and focus on the element.

Figure 9. Ruby browser View Example
[Img = 486,522] http://www.linuxeden.com/upimg/alli%080916/15473918.jpg%/img]


Quick browsing of Ruby resources is another way to increase productivity. In the next section, we will view other new features that help Ruby development in the RadRails plug-in.

[Table = 98%] [tr] [td]

(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // www.linuxeden.com/upimg/allianz 080916/1547390.gif'); ">
[Img = 8, 6] average


Other new features
The Aptana RadRails plug-in adds several enhancements to the previous version of the plug-in, including code completion. For example. type E anywhere in the rb file and press Ctrl + Space. Then, a list of classes, modules, global variables, methods, and variables is displayed when the code of the RadRails plug-in completes. The latest version of RDT also adds new content, such as highlighting global variables and instance/class variables more clearly, enabling and disabling auto-disabling (such as string) functions, JRuby support, SQL Editor, better debugging, square brackets and parentheses, and other content. Aptana's RadRails Pro version even adds more features, including support for new Ruby analysis programs built into the IDE. For more information about the new features of the RadRails plug-in, see V1.0.2 change history. This is the current version at the time of writing this article.
By making significant improvements to Aptana RadRails, it truly shows its power: an important tool for developing Ruby applications. RadRails, combined with other Eclipse plug-ins, can provide first-class IDE, which features comparable to almost all other commercial products.
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.