Creating a Ruby Project - Rachael Boer Photography

Contents
Chapter 1: Installing NetBeans IDE with Ruby Support ..................1
Downloading the IDE ............................................................................. 1
Installing the Java Software Development Kit ...................................... 2
Installing the IDE ................................................................................... 3
Adding Ruby to an Existing NetBeans Installation .............................. 4
Summary.................................................................................................. 6
Chapter 2: Trying Out the IDE .....................................................7
Creating a Ruby Project.......................................................................... 7
Creating a Rails Project ........................................................................ 11
Summary................................................................................................ 16
Chapter 3: Configuring Your Environment ...................................17
Registering Ruby Installations ............................................................. 17
Managing Gems .................................................................................... 20
Using the Ruby Gems Manager .................................................................. 21
Gems Repository Permissions..................................................................... 22
Using the Gem Tool from the Command Line ........................................... 23
Installing Gems That Have Native Extensions ........................................... 25
Adding Servers ...................................................................................... 26
Accessing Databases from the IDE ...................................................... 27
Creating Databases from the IDE................................................................ 27
Registering Existing Databases with the IDE ............................................. 28
NetBeans™ Ruby and Rails IDE with JRuby i
Working with Databases from the IDE ....................................................... 30
Summary................................................................................................ 32
Chapter 4: Working with Ruby Projects.......................................33
Setting Up Ruby Projects ...................................................................... 33
Using the Project and Files Windows .................................................. 36
Adding Files .......................................................................................... 38
Running Ruby Applications.................................................................. 40
Running Rake Tasks ............................................................................. 43
Using Interactive Ruby (IRB)............................................................... 44
Summary................................................................................................ 46
Chapter 5: Working with Rails Projects .......................................47
Setting Up Rails Projects ...................................................................... 47
Working with Databases ....................................................................... 51
Setting Database Configurations ................................................................. 51
Using Databases with JRuby....................................................................... 55
Creating Databases ...................................................................................... 57
Creating and Changing Tables .................................................................... 58
Working with Generators...................................................................... 60
Adding Files .......................................................................................... 62
Running Rake Tasks ............................................................................. 62
Adding Plugins ...................................................................................... 63
Running Applications ........................................................................... 64
Using the Rails Console ........................................................................ 67
Summary................................................................................................ 68
ii
NetBeans™ Ruby and Rails IDE with JRuby
Chapter 6: Editing Files .............................................................69
Live Code Assistance............................................................................. 69
About Semantic Coloring...................................................................... 72
Using Code Completion ........................................................................ 74
Using Live Code Templates .................................................................. 77
About Hints and Quick Fixes ............................................................... 79
Refactoring Code................................................................................... 81
Navigating Code .................................................................................... 86
Navigating Within a File ............................................................................. 86
Navigating to Members ............................................................................... 88
Navigating to Files ...................................................................................... 89
Viewing RDoc........................................................................................ 96
Working with Task Lists ....................................................................... 98
Viewing Recent Changes ...................................................................... 99
Keyboard Shortcuts ............................................................................. 100
Summary.............................................................................................. 102
Chapter 7: Testing and Debugging Projects .............................. 105
Creating Tests...................................................................................... 105
Creating Unit Tests.................................................................................... 106
Adding Fixtures......................................................................................... 109
Creating RSpec Tests ................................................................................ 109
Running Tests and Specs .................................................................... 112
Using Autotest ..................................................................................... 115
Debugging Applications...................................................................... 116
Running the Debugger............................................................................... 117
NetBeans™ Ruby and Rails IDE with JRuby iii
Working with Breakpoints ........................................................................ 119
Using the Debugger Window .................................................................... 122
Using the Local History Feature ........................................................ 123
Viewing Log Files ............................................................................... 124
Summary.............................................................................................. 125
Chapter 8: Working with JRuby................................................ 127
Creating JRuby Projects ..................................................................... 127
Adding Java Libraries to JRuby Projects .......................................... 129
Using Java Classes and Methods in JRuby Projects......................... 130
Setting JRuby Runtime Properties ..................................................... 132
Using JRuby in Java Projects............................................................. 134
Summary.............................................................................................. 137
Chapter 9: Deploying Rails Applications .................................... 139
Creating WAR Files with the Warbler Plugin ................................... 139
Installing the Warbler Plugin .................................................................... 139
Configuring Warbler ................................................................................. 142
Creating the WAR File.............................................................................. 143
Deploying to Application Servers ....................................................... 143
Deploying to the GlassFish Application Server........................................ 143
Deploying to Tomcat................................................................................. 145
Summary.............................................................................................. 145
Chapter 10: Customizing the IDE ............................................. 146
Working with Editor Options.............................................................. 146
Tuning the Code Completion Pop-Up....................................................... 146
Setting Formatting Options ....................................................................... 148
iv
NetBeans™ Ruby and Rails IDE with JRuby
Customizing Live Code Templates ..................................................... 150
Adjusting Hints.......................................................................................... 152
Creating Code Shortcuts............................................................................ 153
Changing Fonts and Colors ............................................................... 155
Modifying Keyboard Shortcuts........................................................... 158
Modifying File Templates ................................................................... 160
Editing a Template .................................................................................... 161
Using Template Properties ........................................................................ 162
Adding Licenses to a Template ................................................................. 164
Setting Task List Patterns ................................................................... 165
Working with Nodes, Files, and Folders............................................ 166
Displaying the Physical File Structure in the Projects Window ............... 167
Adding Ruby Script File Extensions......................................................... 168
Changing the Location of the Ruby Source Files Folder .......................... 168
Editing Hidden Files in the IDE ................................................................ 169
Tweaking Under the Hood.................................................................. 169
IDE Options............................................................................................... 170
Editor Options ........................................................................................... 172
Troubleshooting the IDE .................................................................... 173
Summary.............................................................................................. 174
NetBeans™ Ruby and Rails IDE with JRuby v
Chapter 1: Installing NetBeans IDE with
Ruby Support
Installing the NetBeans IDE is fairly easy. Depending on your operating system, it may be as simple as running a command to fetch the IDE from a repository. Otherwise, you simply download the installation package from the NetBeans web site, ensure that you have the necessary Java
Development Kit (JDK) software on your system, and run the IDE’s installer. If you already have a 6.5 version of the IDE and want to add
Ruby support, skip to the section titled "Adding Ruby to an Existing
NetBeans Installation," later in this chapter.
Downloading the IDE
To get the NetBeans software, go to the NetBeans download page at http://www.netbeans.org/downloads and click the Download
NetBeans IDE button. Choose a language and a platform, then click the
Download button for either the Ruby bundle or the All bundle (see Figure
1-1). Because the Ruby download has a small footprint, it is the best option to choose if you are only doing Ruby programming. If you plan to use the
IDE to develop programs with the other supported languages, the All download bundle is the better choice. You can also choose a different download, such as the Java bundle, and add Ruby support after you install the download. Alternatively, look farther down the web page for the link to the MySQL GlassFish Bundle, which adds the MySQL database server to the download.
Be sure to install version 6.5 or later. Several of the features described in this book do not exist in earlier versions.
NetBeans™ Ruby and Rails IDE with JRuby
1
Figure 1-1. The NetBeans Download Page
Tip You can download and install the IDE and its JDK software dependencies in a single step on an OpenSolaris system using the Package
Manager GUI, or from a terminal window using the following command: pkg install netbeans
. For Linux systems, you might want to check the
Linux repositories to see if they offer NetBeans IDE 6.5 packages.
Installing the Java Software Development Kit
For most of the bundles you must have JDK software on your system before installing the IDE. The NetBeans download page has a link to the
JDK download page as well as to a NetBeans IDE download that includes the JDK. On Ubuntu, you can install the JDK 6 package from Multiverse.
If you have Ubuntu 8.04 or later you can also use the Open JDK 6 packages from the Universe repository.
2
NetBeans™ Ruby and Rails IDE with JRuby
Tip If you install the Ruby-only version of the IDE and will not use any of the IDE’s Java features, you can install the Java Runtime
Environment (JRE) instead of the JDK. However, if you do not have the JDK you will not be able to use the Warbler plugin to generate WAR files. The
JDK includes the JAR command, which is used by Warbler.
The installation instructions on the download page provide steps for the various operating systems. For example, on Windows you run the JDK installer.
Although you can install the IDE using JDK 5.0 software, installing with the JDK 6 release or later is recommended. For example, the LCD subpixel antialiasing changes in version 6 greatly improve the way fonts are displayed in the IDE on Windows. If you are on Ubuntu you should install, at a minimum, version 6 update 10 of the JDK software due to Abstract
Window Toolkit (AWT) issues with the Beryl and Compiz desktops.
Tip If you are not sure of your JDK version, execute the following in a command window: java-version
.
Installing the IDE
When you have downloaded the NetBeans installer and have the necessary
JDK installed on your system, you are ready to install the IDE. The method for installing depends on your operating system, as described here:
Windows: Double-click the file that has the .exe
extension.
Solaris, OpenSolaris, and Linux platforms (assuming you haven’t
installed from a repository): The installer file has an
.sh
extension. Before you run the installer file, execute the following command to make the file
NetBeans™ Ruby and Rails IDE with JRuby
3
executable: chmod +x ./<installer-file-name>
. On Ubuntu, make sure your JDK release is the 6 update 10 release or later. Otherwise you could get a blank screen.
Mac OS X: The installer file has a .dmg
extension. After you run the installer the panel that opens displays a package icon with an
.mpkg
extension. Click this icon to start the installation wizard.
When the installer displays the JDK option, make sure the installer is using the JDK 6 release or later. If the installer cannot locate the JDK installation you want to use, try starting the installer again, but this time pass
–-javahome <path-to-jdk> to the installer.
The Ruby, Java, and All bundles include the GlassFish V3 server. The default username is anonymous and the password is blank. The Java and
All bundles also include the GlassFish V2 server. The default username and password for the V2 server are admin and adminadmin.
The download instructions on the NetBeans download page provide more detailed instructions for each operating system.
Tip If you upgrade the JDK after you install the IDE, you might want to open the
netbeans-install-dir
/etc/netbeans.conf
file and edit the netbeans_jdkhome
property to point the new installation.
Adding Ruby to an Existing NetBeans Installation
If you have a NetBeans IDE 6.5 installation without Ruby support (that is, there is no Ruby category when you go to create a new project), you can easily add Ruby support to the IDE by doing the following:
1. If your network uses a proxy, choose Tools h Options from the main menu, select Manual Proxy Settings, type the HTTP Proxy and Port for your proxy, and click OK.
4
NetBeans™ Ruby and Rails IDE with JRuby
2. Open the Plugins manager by choosing Tools h Plugins from the main menu.
3. Click the Available Plugins tab and click the Category header to sort the list.
4. Select the check box next to the Ruby modules of interest (see Figure 1-2). At a minimum, select Ruby and Rails, which provides all of the NetBeans support for Ruby and Ruby on Rails (projects, editing, and so forth).
5. Unless you already have a Ruby or JRuby runtime installed, you will also want to select JRuby and Rails Distribution.
6. The other plugins in the Ruby category are optional, but there's really no reason not to install them as well, especially the GlassFish V3 JRuby
Integration plugin.
7. Click the Install button to launch the Plugin Installer.
8. Accept the license and click Install to complete the installation.
Figure 1-2. Plugins Manager
NetBeans™ Ruby and Rails IDE with JRuby
5
Summary
In this chapter, you learned how to obtain and install the NetBeans 6.5 IDE with Ruby support, as well as the JDK software. The installation process is fairly automated, regardless of whether you are developing on Windows,
Mac, Solaris, or some flavor of Linux. In addition, the IDE enables you to install a Ruby-only version or a different configuration with added Ruby support.
6
NetBeans™ Ruby and Rails IDE with JRuby
Chapter 2: Trying Out the IDE
At this point you are probably anxious to give the IDE a test run. Since the
NetBeans IDE provides everything you need to create Ruby and Rails applications out of the box, we thought we’d take you for a quick test drive before we dive into the details. As we go along through this chapter, we’ll reference the chapters that go more in-depth on various topics.
Creating a Ruby Project
Follow these steps to create a new Ruby project:
If you haven’t done so already, start the IDE by clicking the NetBeans icon.
1. To create a project, select File h New Project from the main menu. A New
Project wizard appears from which you specify the type of project you want to create.
2. Select Ruby in the Categories list, select Ruby Application in the Projects list
(see Figure 2-1), and click Next.
3. For this tour, use all the default settings. Note the assigned Project Name and
Project Location and click Finish. You will learn more about using the New
Project wizard for Ruby projects in Chapter 4.
NetBeans™ Ruby and Rails IDE with JRuby
7
Figure 2-1. Choosing the Project Type
4. On the left side of the IDE is a panel that contains tabs for the Projects, Files, and Services windows. You will work in the Projects window (see Figure 2-
2). If you don't see the Projects window click the Projects tab to display it, or select Window h Projects from the main menu. You should now see your
Ruby project in the Projects window, with the main.rb node nested under the
Source Files node.
8
NetBeans™ Ruby and Rails IDE with JRuby
Figure 2-2. Projects Window
Note The Projects window shows a project's logical view; its pop-up menu contains actions you typically use to develop applications. The Files window shows the project's physical structure and has a pop-up menu for managing files.
5. In the main section of the IDE is the editor window. After the IDE creates the project it opens the
main.rb
file in this window (see Figure 2-3). The code in this file calls the
puts
method to display the string “Hello World.” You will learn how to use the editor in Chapter 6.
NetBeans™ Ruby and Rails IDE with JRuby
9
Figure 2-3. Editor Window
6. To run the application, right-click the project’s node in the Projects window and choose Run from the pop-up menu. The IDE displays the output in the
Output window at the bottom of the IDE (see Figure 2-4).
Figure 2-4. Output Window
7. You can delete this project by right-clicking the project’s node in the Projects window, then choosing Delete from the pop-up menu, selecting the Also
Delete Sources check box, and clicking Yes.
10
NetBeans™ Ruby and Rails IDE with JRuby
Creating a Rails Project
Now follow these steps to create a Rails project:
1. Select File h New Project from the main menu.
2. Select Ruby on Rails Application from the Projects list and click Next.
3. The New Project wizard for a Rails application has more steps, which you will learn about in Chapter 5, but we will skip them for this project. As you did when creating the Ruby project, make note of the Project Name and
Project Location and click Finish. As part of the project creation process, the
IDE runs the rails
command to create a new Rails project and displays the output in the Output window (see Figure 2-5). The output shows the names of the created files as links. You can open any of these files in the editor by clicking the file's link.
Figure 2-5. Rails Command Results in the Output Window
NetBeans™ Ruby and Rails IDE with JRuby
11
4. For this project you need to edit the environment.rb
file to remove the
Active Record framework from the environment configuration. Because you are not using a database in this application, you need to prevent Rails from trying to access the database specified in the database.yml
file. Scroll through the Rails output until you see the line that says create config/environment.rb
and then click the line to open the file in the editor window. Scroll to the following line in the source (around line 21):
# config.frameworks -= [ :active_record,
:active_resource,
:action_mailer ]
Tip To display line numbers, right-click in the left margin of the editor and choose Show Line Numbers from the pop-up menu.
5. Click the config.frameworks
line and click the Uncomment button on the editing tool bar to remove the
#
character from that line.
6. An asterisk (*) on the tab indicates the file has been modified. Click the Save
All button on the main toolbar to save your changes.
7. If you are using a Rails 2.1 version, which is the case with the bundled JRuby software, you must also remove the Active Record references in new_rails_defaults.rb
. Scroll through the Rails output until you see that line that says create config/initializers/ new_rails_defaults.rb
, then click that line to open the file in the editor window. Select each of the two
ActiveRecord::Base
settings and click the Comment button to comment out the lines, as shown here:
#ActiveRecord::Base.include_root_in_json = true
#ActiveRecord::Base.store_full_sti_class = true
8. Click the Save All button on the main toolbar.
9. Now look at the Projects window, which shows a logical view of the Rails project, as shown in Figure 2-6.
12
NetBeans™ Ruby and Rails IDE with JRuby
Figure 2-6. Rails Project in the Projects Window
10. For this simple project you will create a controller and a view. In the Projects window, right-click the Controllers node and choose Generate from the popup menu. The IDE displays the Rails Generator dialog box with controller selected in the Generate drop-down list (see Figure 2-7). Set the Name to
Welcome, set the Views to index, and click OK. The Rails Generator dialog box is a GUI interface to the Rails generate
command. You will learn more about the Rails Generator in Chapter 5.
NetBeans™ Ruby and Rails IDE with JRuby
13
Figure 2-7. Rails Generator
11. The IDE opens the welcome_controller.rb
file in the editor. Right-click in the file's source and choose Run File from the pop-up menu. The IDE starts the server, runs the index action in the controller, and displays the index view in your browser. You should see a page that looks similar to Figure 2-8.
14
NetBeans™ Ruby and Rails IDE with JRuby
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project