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

Microsoft
Here are some of the key reasons why you would want to run Unified Access Gateway with DirectAccess.
Microsoft
Review how one energy firm tightened protection and simplified IT work using business-ready security solutions.
Sybase
In this white paper, IDC analyzes the role of next-generation mobile enterprise platforms as organizations seek a more strategic deployment of mobile solutions.

Learn the important issues you must consider before starting your next mobility initiative. Get your mobility white paper from IDC now, compliments of Sybase.

What People Are Saying

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!  

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!

Manager Experience Demo
Go beyond self-service solutions to perform more effectively. Watch Now.


IT Jobs