Introduction To Ruby

Discussion in 'Ruby on Rails' started by pradeep, May 27, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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



    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.
     
    coderzone likes this.
  2. Magena

    Magena Banned

    Joined:
    Jul 12, 2008
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    Hi

    Nice article and i need this. Is ruby better than PHP? Or they are different with each other.

    thanks
     
  3. vikas1234

    vikas1234 New Member

    Joined:
    Aug 21, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    I think its between php and perl .... can you please provide me an example where I should use ruby instead of php
     
  4. dimpi

    dimpi New Member

    Joined:
    Jul 10, 2009
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    0
    Hello friends.............

    Great. Nice Article.Thanks for sharing this information.I will try.This article is very useful. Thanks alot.:nonod:
     
  5. smithshn

    smithshn New Member

    Joined:
    May 3, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. Lulugreen

    Lulugreen New Member

    Joined:
    May 23, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your detailed post! Good luck to you!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice