Ads by TechWords

See your link here
Receive the latest technology news and information.
Application/Web Development
Computerworld Daily News (First Look and Wrap-Up)
Computerworld Blogs Newsletter
The Weekly Top 10
Cloud Computing
View all newsletters




Privacy Policy
 

Rails for Java Developers

Using the Rails framework, Ruby is giving Java a run for its money

May 3, 2007 12:00 PM ET

Computerworld - 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 Person
That'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
Podcast:
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




Jump to comments

Java

Additional Resources

WHITE PAPER
Approximately 60 percent of data migration projects overrun time or budget, while some fail completely. Download this white paper, "Enhancing Your Chance for Successful Data Migration," to learn the critical steps you need to take to execute a data migration project with minimum cost and risk to your business.
WHITE PAPER
Read the Gartner research note to learn why the TCO of a server-based computing deployment used to deliver all applications to users is around 50% lower than that of an unmanaged desktop deployment.
WHITE PAPER
Economic downturns have a tendency to accelerate emerging technologies, boost the adoption of effective solutions, and punish solutions that are not cost competitive or that are out of synch with industry trends. This IDC White Paper presents the results of an IDC survey of 330 companies in Western Europe, Asia/Pacific and the Americas that measures the receptiveness to Linux and takes into consideration changing views driven by the disruptive economic environment that businesses face today.

What People Are Saying