Rails for Java Developers
Using the Rails framework, Ruby is giving Java a run for its money
May 3, 2007 12:00 PM ETComputerworld -
This article is excerpted from Rails for Java Developers , by Stuart Halloway and Justin Gehtland, with permission of The Pragmatic Programmers. All rights reserved.
Defining Classes
In both Java and Ruby, classes encapsulate behavior and state. Simple classes typically contain the constructs shown in Figure 2.1. To demonstrate these constructs, we will build Java and Ruby versions of a Person class. These Persons have simple state: a first name and a last name. They also have simple behavior: They can marry another Person, resulting in both Persons sharing a hyphenated last name. The next sections build Persons one step a time; for complete listings, see Figure 2.2 and see Figure 2.3.
Declaring Fields
The Java Person begins with a class declaration and some fields:
public class Person {
private String firstName;
private String lastName;
Fields are usually marked private so they can be accessed only from within the class. This way, you can change the underlying representation later. For example, you could store the entire name in a fullName field, and only other code that might change would be within the class itself.The Ruby
Person begins simply with a class declaration:class PersonThat's it. There is no need to declare instance variables (the Ruby equivalent of fields) because they come into existence automatically when they are used.
The other attributes of a declaration (types and protection modifiers) are irrelevant in Ruby. Ruby instance variables do not need a type such as
String, because they accept any type. Ruby instance variables are implicitly private, so this designation is unneeded as well.Defining Constructors
The Java definition continues with a constructor that sets the initial values of the fields:
public Person(String firstName,
String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Constructors have the same name as their class and are often marked public so that all other classes can use them. Notice that the constructor arguments share the same names as the private fields: firstName and lastName. To disambiguate, you explicitly prefix the instance variables with this. The Ruby declaration also continues with a constructor that sets the initial value of instance variables:
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
|
|
| Related Podcast: Author Justin Gehtland explains the similarities and differences of everything from syntax to testing to scalability in this interview with Online Projects Editor Joyce Carpenter. Duration: 25 minutes |
Java
Additional Resources



Learn the important issues you must consider before starting your next mobility initiative. Get your mobility white paper from IDC now, compliments of Sybase.
White Papers & Webcasts
Extend, Replace, or Convert; which is the best way forward for COBOL Applications?
Download this white paper, free, compliments of Micro Focus!
Data in Action: Making the Planet Smarter
Register Now
Oracle Accelerate - Not Just Smart but Timely
Download Now!
Why BI is Ripe - Now! - For Businesses of Any Size
Download Now!
The Workday User Experience Video
Watch Workday's Creative Director, Scott Lietzke, discuss the business-centered design philosophy at Workday.
Business Process Framework Demo
Learn about Configurable Business Processes and Calculated Fields. Watch Now!
Rapid Implementation: The New Age of ERP
Download Now!
Manager Experience Demo
Go beyond self-service solutions to perform more effectively. Watch Now.

