The Ruby-interpreted scripting language is an easy-to-learn environment for quickly developing object-oriented applications. Learn more about Ruby in this article from Builder.com Most frequently compared to Smalltalk, Python, Perl, C++, and Java, Ruby is a useful tool for rapid development and creation of prototypes. This article introduces Ruby and provides a simple script to illustrate its familiar and powerful syntax. Ruby highlights Ruby was created in 1995 by Japanese national Yukihiro Matsumoto as a way to easily process text and create system management tools. Since then, it has rapidly grown in popularity, thanks to its unique object-handling and inheritance structure. In Ruby, everything is an object. For example, if you were to assign a variable with a value of 42, the number 42 itself is an instance of the integer object class, which is then stored as a value. Standard classes are user-extensible, giving the language ultimate flexibility. Additionally, C++ and Ruby classes can be inherited on the fly, enabling applications to make use of functions that Ruby itself doesn't provide. All variables are assigned and typed at run time. This eliminates the need for predefining variables, although mechanisms are available for specifically typing values if necessary. This also means that special syntax is not used to tell Ruby what type a variable is. For example, while @varname represents an array in Perl, typing is purely contextual in Ruby. In contrast, syntax is used to control a variable's scope. For example, variable names beginning with a lowercase letter are local variables; those beginning with a capital letter are constants; those beginning with $ are global; and those beginning with @ are instance variables. Other scope variables exist, along with several special global variables. Ruby allows only single inheritance. This may sound limiting at first, but it means that naming clashes and other issues stemming from multiple inheritance are avoided altogether. Ruby does provide a mechanism for overcoming the perceived difficulties single inheritance may cause, called "mixing in," where methods from other classes may be included into a subclass that is then inherited. Other features that help define Ruby as a language include garbage collection, method overloading, and dynamic typing and definitions. Let's walk through the sample script for a look at Ruby's syntax. Sample script This script is run as an interactive session in a DOS or UNIX shell. I've outlined some of the more basic constructs of Ruby, but a wealth of user-defined loops and other interesting features is available. First, we begin with a simple prompt. Notice that Ruby has no line termination syntax. Code: print "What is your favorite color?: " Next, the script uses the predefined gets method to get a string from standard input. If an error occurs and the variable favcolor cannot be defined, the script exits. Code: favcolor = gets exit if not favcolor The chomp! function removes the trailing return character from the string. The bang (!) symbol signifies to the programmer that this is a destructive method, meaning it will modify the value of favcolor. Code: favcolor.chomp! The case statement evaluates favcolor and performs an action that depends on its value. Notice the lack of parentheses and braces. The end of the loop is signified by the end keyword. Code: case favcolor when "red" print "You must like cherries!\n" when "blue" print "You must like the ocean!\n" when "green" print "Green is my favorite, too!\n" else print "Your favorite color is ugly. Goodbye.\n" exit end Another type of loop, unless, evaluates the same as the statement, if favcolor != "green". Code: unless favcolor == "green" The \ symbol below continues the command on the next line. The #{ } syntax tells Ruby to evaluate the contents. Code: print "I don't like #{ favcolor }, " \ "so I don't want to play anymore.\n" If that statement is parsed, the script continues and discovers there is nothing left to do, so it performs garbage collection and exits at the end of the file. To explicitly exit here, you could use an exit statement. If the unless statement isn't satisfied, the script continues with the else clause. Code: else print "Will you be my best friend? (y/n): " Again the script grabs user input, chomps it, and looks for an error. Code: friend = gets friend.chomp! exit if not friend This while loop will prompt the user continuously until it gets a y or n value and matches the regular expression, /[yn]/. The prompt above could have been included here, guaranteeing that the loop would fail the first time through before getting user input, but I wanted to put in a different message. Code: while friend !~ /[yn]/ print "I said, will you be my best friend? (y/n): " friend = gets friend.chomp! exit if not friend end When the script finally gets an acceptable answer, it executes this if statement. Code: if friend == "n" print "You're mean. I'm leaving.\n" exit else print "Great! I'll be over " \ "in a second - see you soon!\n" exit end This ends the unless loop we started above. At the end of the file, the script exits. end Where to get Ruby The Ruby home page offers the latest versions, documentation, and links to other useful Web sites. Ruby Language Reference Manual is a syntax and usage guide. The Pragmatic Programmers Web site offers the Ruby for Windows download. The Ruby Language FAQ, from rubycentral.com, includes useful installation instructions with download links. The Ruby Way is a reference by Hal E. Fulton and Guy Hurst. Ruby in a Nutshell is an O'Reilly book by Yukihiro Matsumoto. Rare gem In the forward to the book The Ruby Way, Matsumoto states that in developing Ruby, he "followed the Principle of Least Surprise" to create a scripting language that is "human-oriented" and that feels natural to developers. Indeed, Ruby provides a straightforward, logical methodology for quickly achieving the solution you seek.
Hi Nice article and i need this. Is ruby better than PHP? Or they are different with each other. thanks
I think its between php and perl .... can you please provide me an example where I should use ruby instead of php
Hello friends............. Great. Nice Article.Thanks for sharing this information.I will try.This article is very useful. Thanks alot.:nonod:
Good description of your article. Such Ruby is a useful tool for development and creation of prototypes. Your suggestion is really useful and better way to explain with example.