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



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!
Southern Company
Download Now
Extending Client Refresh - 11 Steps to Maximize Savings
Register Now!
Defending Against the Storm
Download Now
Lower the Cost and Complexity of a Mobile Workforce through Automation
Download This Resource Now!
Share our Strength
Download Now
Managing Mobility: Improve Data Security, Compliance and Manageability
Download This Resource Now!
Consolidate Your Servers and Storage to Lower Costs with Oracle Database 11g
Register for this webcast!
