Rails for Java Developers
Using the Rails framework, Ruby is giving Java a run for its money
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 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 |



- Excel 2010 Cheat Sheet
- Register for this Computerworld Insider Cheat Sheet and gain access to hundreds of premium content articles, guides, product reviews and more.
- The Keys to Distributed & Agile Application Development
- How leading firms are winning with strategies for efficient application development, without relying on co-location.
- Overcome Top 7 Admin Challenges of Active Directory
- As Active Directory's role in the enterprise has drastically increased, so has the need to secure the data. Gain insight on creating repeatable,...
- Insiders Can Ruin Your Company. Take Action.
- Did you know that 80 percent of threats to an organization come from the inside? The threat from insiders is often overlooked in...
- Top Solutions and Tools to Prevent Devastating Malware
- Custom malware frequently goes undetected. According to Forrester Research, the best way to reduce risk of breach is to deploy file integrity monitoring...
- Streamline Compliance and Increase ROI
- Streamline, simplify, and automate compliance related activities; especially those that impact multiple business units. This white paper from NetIQ, outlines solutions that will... All App Development White Papers
- Reduced TCO for Communications Applications with New Oracle SPARC Servers
- In this webcast learn how Oracle's new SPARC T4 servers and SPARC Supercluster deliver the security, performance, and scalability required for 4G network...
- Optimizing Networks for the Cloud
- Join guest speaker, Rohit Mehra, IDC Director of Enterprise Communications Infrastructure, to explore current trends, discuss best practices for optimizing Data Center and...
- Apps QuickStart Series Part 2: Designing and Deploying SQL Server on VMware vSphere
- Download this webcast to learn about the design considerations for virtualizing SQL workloads, performance and scalability information and high-availability options, as well as...
- Apps QuickStart Series Part 1: Designing and Deploying Exchange 2010 on VMware vSphere
- Download this webcast to learn the virtual hardware design considerations for Exchange 2010, deployment using the building block approach, options for high-availability and...
- Customer Spotlight: How IPC The Hospitalist Company Implemented Oracle on VMware
- Have you been looking to hear about customer's experiences with the new VMware vCenter Site Recovery Manager product? View this webcast to learn... All App Development Webcasts