This tutorial assumes:
The Ruby Tutorial demonstrates how to use Komodo to write and debug a simple Ruby program which saves its data in a YAML file. In this tutorial you will:
See Debugging Programs for more information on debugging in Komodo.
From the Help > Tutorials menu, click Ruby Tutorial, or open the rubytutorial.kpf_ file from the samples/rubytutorials_ subdirectory of Komodo's user data directory
The tutorial project will open in the Places sidebar.
The rubytutorial_ project contains:
In the Places sidebar, double-click the menagerie.rb file. This file opens in the Editor Pane; a tab at the top of the pane displays the filename.
The menagerie.rb program is a simple interactive program that can add, list, and search for information about animals. It can also store this information in a YAML file (e.g. animals.yaml). It is essentially an extremely simple database with predefined fields.
In this step, you will analyze the program in sections. Ensure that line numbers are enabled in Komodo (View|View Line Numbers) and that the file menagerie.rb is displayed in the Komodo editor.
|**Komodo Tip**: Notice that syntax elements are displayed in different colors. You can adjust the display options for language elements in the [Preferences](../prefs#prefs_top) dialog box.|
The Entry class contains variables which hold data for individual entries in our simple database, and the behavior of that data. Information about each animal in our menagerie will be contained in an Entry class object.
class Entrydeclares the class
These variables need to be private (instance variables) rather than global because there will be an instance of the Entry class for each animal in the menagerie.
@time_modas read-only attributes of the Entry class.
@time_modis updated with the current time whenever
|**Komodo Tip**: Click on the minus symbol to the left of line 18\. The section of code for the **sci_name** method is collapsed. Doing this at line 8 collapses the entire **Entry** class. This is called [Code Folding](../editor#Folding).|
The Menagerie class contains the @menagerie hash: the container which holds the multiple instances of the Entry class. It also contains all the methods available to the user of the application (via the command interface starting at line 154.
class Menageriedeclares the class
The name of the animal is stored as a key in the
@menagerie hash. It references the Entry object which contains the rest of the information, namely the scientific name (
sci_name) and description (
descr). All the information we store on each animal is kept in two separate areas -- the
@menagerie key and the Entry it points to.
|**Exercise**: After completing the tutorial, come back to this section and try writing a more "user friendly" version of this **cmd_add** method which asks for the name, scientific name and description separately (i.e. one at a time).|
@menagerie's key for a match
name) has been given
deletemethod removes the entry
dumpmethod of the YAML class (see line 6) to show the entire Menagerie object in YAML format
fnameargument is "!"; this sets the
overwritevariable which determines whether cmd_save can overwrite an existing file
overwriteis false and the specified file exists, warn the user with a helpful message.
fnameargument exists, set the
@fnameinstance variable, if not, prompt the user with a useful error message.
fd) is created on line 124 which is written to in the next line (a YAML dump of our menagerie)
The program needs a way to handle input from the user. This section deals with parsing input and splitting it into commands which call methods in the Menagerie class, and arguments for those methods.
cmdis a valid method in the Menagerie class.
In this step you will run the program and interact with it.
The output of the last command (including "Error: undefined method...") indicates that something is not working correctly. Debugging can help trace this problem. Type
quit in the Output window before starting the debugger.
In this step you will add breakpoints to the program and "debug" it. Adding breakpoints lets you run the program in sections, making it easier to watch variables and view the output as it is generated.
Breakpoints are used to identify exactly where a program may be having problems. A bug has been intentionally left in the cmd_delete method which can illustrate this.
|**Komodo Tip**: Debugger commands can be accessed from the **Debug** menu, by shortcut keys, or from the Debug Toolbar. For a summary of debugger commands, see the [Debugger Command List](../debugger#How_use_Debugger).|
@menageriehash. Expand the view of the hash by clicking the plus symbol next to it. It should only contain the "Leopard gecko" entry after the "Komodo dragon" entry has been deleted.
@menagerie.has_key?(name))' on line 92 and drag it into the Watch tab. You can place most valid Ruby expressions in this area, including assignments, whether they're in the program or not. For example, placing the expression '
x = 2 + 2' in the Watch tab will show that a local variable called 'x' is set to 4.
|**Komodo Tip**: What do the debugger commands do? * **Step In**: Executes the current line of code and pauses at the following line. * **Step Over**: Executes the current line of code. If the line of code calls a function or method, the function or method is executed in the background and the debugger pauses at the line that follows the original line. * **Step Out**: When the debugger is within a function or method, Step Out executes the code without stepping through the code line-by-line. The debugger stops on the line of code following the function or method call in the calling program.|
status_messagevariable. This variable contains the message that will be returned to the user.
#<Entry:0x83096f8>Error: undefined method' for #Entry:0x83096f8
" which is not a very user-friendly message. The **cmd_delete** method is missing an explicitreturn` value to provide if the deletion was successful.
There are many Ruby tutorials and beginner Ruby sites on the Internet, including:
© 2017 ActiveState Software Inc. All rights reserved. ActiveState®,
Komodo®, ActivePerl®, ActivePython®, and ActiveTcl® are registered
trademarks of ActiveState.
All other marks are property of their respective owners.