Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby—A Diamond of a Programming Language? : Page 4

Ruby is an object-oriented, meta-programming language that has many developers wondering if there are actually better alternatives to languages like Java and C#.


advertisement
Building Applications with Interactive Ruby
So now you have a little bit of knowledge to start building a Ruby application. To demonstrate the interactive nature of the Ruby interpreter and to see how applications slowly get developed in Ruby, start an Interactive Ruby Help and Console utility (the fxri utility installed with Ruby) as shown in Figure 6.

Figure 6. Start fxri: Open the Interactive Ruby tool fxri from the Windows Start menu as shown here.

In the lower right hand pane of the window is an interactive Ruby command prompt (notice the irb(main) :001:0> command prompt) that displays when the window opens. Go ahead and enter the Figure 7 at the command prompt.



Notice that as you type in your code, the interpreter is helping to format your code and as you enter your last "end" statement, Ruby returns => nil which is an indication that the Ruby interpreter has detected the end of your Rectangle class. Rectangle is now defined in this irb session.

Now try to create a few instances of your new Rectangle class. On the very next line, enter Rectangle.new(4,5) and then Rectangle.new(5,12). You should see Ruby again respond with new instances of your Rectangle:

irb(main):011:0> Rectangle.new(4,5) => #<Rectangle:0x58d2ee8 @width=5, @height=4> irb(main):012:0> Rectangle.new(5,12) => #<Rectangle:0x58cfdc8 @width=12, @height=5>

Now let's try that area method. On the next irb command prompt line enter Rectangle.new(6,5).area(). You should see results that look like the following:

irb(main):013:0> Rectangle.new(6,5).area() => 30

Cool?! Yes, and extremely powerful as you slowly build and test chunks of your application classes and methods. But wait, there is more.

In Ruby, classes are never closed. This means you can always add or redefine methods to an existing class. Say, for example, that you also wanted to add a circumference method to the Rectangle class that you created. On the command prompt, enter the following code a line at a time:

class Rectangle def circumference () @height * 2 + @width * 2 end end

Figure 7. Enter the Rectangle Class: Enter the definition of the Rectangle class into the fxri interactive Ruby interpreter as shown in the lower right-hand frame of this example.

Has the Rectangle class just been defined again? No, the Ruby interpreter understands that you are making a modification to the current Rectangle class and it is adding a new method to the existing Rectangle class. Now enter the following line at the command prompt: Rectangle.new(2,3).circumference(). You should see results that look similar to those below:

irb(main):014:0> class Rectangle irb(main):015:1> def circumference() irb(main):016:2> @height * 2 + @width * 2 irb(main):017:2> end irb(main):018:1> end => nil irb(main):019:0> Rectangle.new(2,3).circumference => 10

To redefine any method already in the Rectangle class, for example the area method, simply reenter the class definition with the new area method definition:

irb(main):020:0> class Rectangle irb(main):021:1> def area() irb(main):022:2> @height*2 irb(main):023:2> end irb(main):024:1> end => nil irb(main):025:0> Rectangle.new(6,5).area => 12

In the simple example above, the area method has been redefined to always return twice the height.

The idea that a class is never closed applies to classes you define as well as those built into the language. For example's sake, let's add an area method to the String class. Enter the following code at the command prompt:

class String def area() length() end end

In this case, you are defining the "area" of a string to return the length of the string. Now, try this new method on your name as a String as shown with im below:

irb(main):026:0> class String irb(main):027:1> def area() irb(main):028:2> length() irb(main):029:2> end irb(main):030:1> end => nil irb(main):031:0> "Jim".area => 3

As you read this article, use the interactive nature of Ruby and its development environment to test and explore the programming language with the small chunks of Ruby code provided to you.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap