RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


A Java Developer's Guide to Ruby : Page 2

Ruby's versatility and flexibility complement Java well. That's why a Java developer who can program in Ruby is more effective and efficient than one who programs only in Java.

Ruby String Handling
The Ruby String class provides a large set of string-processing methods that are more flexible than Java's string handling capabilities. This section shows a useful subset of Ruby's string processing. This code snippet shows how to combine strings, take them apart with slices, and then search for substrings (the examples to follow use the # character to make the rest of a line a program comment):
require 'pp'  # use the "pretty print" library. Defines the function 'pp'

# define some strings to use in our examples:
s1 = "The dog chased the cat down the street"
s2 = "quickly"

puts s1
puts s1[0..6]  # a substring slice up to and including character at index==6
puts s1[0...6] # a substring slice up to (but not including) the character at index==6

puts "He is a #{s2} dog #{1 + 6} days a week." # expressions inside #{} are inserted into a double quote string

puts "   test  ".strip  # create a copy of the string: the new copy has white space removed

puts s1 + ' ' + s2 # string literals can also be formed with single quotes
puts s2 * 4

puts s1.index("chased") # find index (zero based) of a substring

s1[4..6] = 'giant lizard'  # replace a substring (/dog/ -> /giant lizard/)
puts s1

s2 = s2 << " now"  # the << operator, which also works for arrays and other collections, copies to then end
puts s2

puts "All String class methods:"
pp s1.methods # the method "methods" returns all methods for any object

The output would be:

The dog chased the cat down the street
The dog
The do
He is a quickly dog 7 days a week.
The dog chased the cat down the street quickly
The giant lizard chased the cat down the street
quickly now
All String class methods:
 "inspect", ......]    # most methods not shown for brevity--try this in irb

The << operator in the above example is really a method call. When evaluating expressions, Ruby translates infix operators into method calls. For example, the << operator in the following code adds the value of the expression on its right side to the value on the left side:

>> "123" << "456"
=> "123456"
>> "123".<<("456")
=> "123456"
>> 1 + 2
=> 3
>> 1.+(2)
=> 3

In the above example, using the form ".<<" is a standard method call.

Many classes use the << operator to add objects to a class-specific collection. For example, you will later see how the Ferret search library (a Ruby gem you have installed) defines the << operator to add documents to an index.

Modifying an Existing Class
The key to Ruby's versatility is the ability to extend all its classes by adding methods and data. I frequently extend core Ruby classes in my application, not in the original class source code. This likely seems strange to Java or even C++ developers, but this technique lets you keep resources for a project in one place and enables many developers to add application-specific functionality without "bloating" the original class. As a Java programmer, think how the limitations of Java constrain you: if you want to add functionality and data to an existing class, you must subclass.

The following listing shows how to add the method stem to the String class:

  puts "The trips will be longer in the future".downcase.stem # stem is undefined at this point
    puts 'Error:' + $!
require "rubygems"
require_gem 'stemmer'

class String # you will extend the String class
    include Stemmable # add methods and data defined in module Stemmable

puts "The trips will be longer in the future".downcase.stem

You will also find it useful to add methods and perhaps new class instance variables to existing classes in your application.

The next section looks at "duck typing," another example of the extreme flexibility that Ruby offers.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date