Ruby Programming/Overview

From Wikibooks, the open-content textbooks collection

< Ruby Programming
Jump to: navigation, search

| Installing Ruby →


Ruby is an object-oriented scripting language developed by Yukihiro Matsumoto ("Matz"). The main web site for Ruby is ruby-lang.org. Development began in February 1993 and the first alpha version of Ruby was released in December 1994. It was developed to be an alternative to scripting languages such as Perl and Python.[1] Ruby borrows heavily from Perl and the class library is essentially an object-oriented reorganization of Perl's functionality. Ruby also borrows from Lisp and Smalltalk. While Ruby does not borrow many features from Python, reading the code for Python helped Matz develop Ruby.[1]

Mac OS X comes with Ruby already installed. Most Linux distributions either come with Ruby preinstalled or allow you to easily install Ruby from the distribution's repository of free software. You can also download and install Ruby on Windows. The more technically adept can download the Ruby source code[2] and compile it for most operating systems, including Unix, DOS, BeOS, OS/2, Windows, and Linux.[3]

Contents

[edit] Features

Ruby combines features from Perl, Smalltalk, Eiffel, Ada, Lisp, and Python.[3]

[edit] Objects and mixins

Unlike C/C++, Ruby is a pure object-oriented language. Everything is an object, including numbers and other primitive types. An object's properties are called instance variables and the functions associated with an object are called its methods.[3]

Ruby intentionally only allows single inheritance. Instead of multiple inheritance, Ruby programmers can mixin a module to receive all of its methods, similar to the Categories feature in Objective-C. Ruby programmers often find mixins to be simpler and more powerful than multiple inheritance.[3]

[edit] Flexibility

In Ruby, everything is malleable. Methods can be added to existing classes without subclassing, operators can be overloaded, and even the behavior of the standard library can be redefined at runtime.

[edit] Variables and scope

You do not need to declare variables or variable scope in Ruby. The name of the variable automatically determines its scope.

  • x is local variable (or something besides a variable)
  • $x is a global variable
  • @x is an instance variable
  • @@x is a class variable

[edit] Blocks (closures)

Blocks, also referred to as closures, are one of Ruby's most powerful features.[4] They are similar to Java's anonymous classes but are easier to use.

Closures allow you to pass a block of code to a method. A common example is to call a sort method and to pass (or attach) a closure that compares two values -- this closure determines how the items are sorted. The closure might compare the values alphabetically or numerically. The closure might also do something complicated. If the values being sorted are product IDs, the closure could retrieve product names from a database and then compare the product names instead of the product IDs.

[edit] Advanced features

Ruby contains many advanced features.

You can also write extensions to Ruby in C or embed Ruby in other software.

[edit] References

  1. 1.0 1.1 Bruce Stewart (November 29, 2001). An Interview with the Creator of Ruby. O'Reilly. Retrieved on 2006-09-11.
  2. Download Ruby. Retrieved on 2006-09-11.
  3. 3.0 3.1 3.2 3.3 About Ruby. Retrieved on 2006-09-11.
  4. Bill Venners (December 22, 2003). Blocks and Closures in Ruby. artima developer. Retrieved on 2006-09-11.
Personal tools
Create a book
  • Add wiki page
  • Collections help