Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Ruby for the Newbie
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Ruby for the Newbie
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-10-15

    Table of Contents:
  • Ruby for the Newbie
  • A Few Notes About Formatting
  • Variables
  • Constants

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Ruby for the Newbie - Variables


    (Page 3 of 4 )

    Variables are storage units that hold data. The data ranges from numbers, to text (strings), to a multitude of other data. In order for a variable to work, you must name it (otherwise you cannot refer to it ) and place the data within it. Note that the data held in a variable can change, and so can the data in a constant (discussed later), unlike in other programming languages.

    When naming a variable, there are several things to keep in mind. The basic convention for naming a variable is that if there is more than one word, separate them by using the underscore (example: my_variable). Typically variables are lowercase, start with a letter, and can contain letters, numbers, underscores, and capital letters. Any name you choose is fine so long as it follows those conventions and does not use one of Ruby's reserved words, shown in the table below:

     

    BEGIN

    END

    _file_

    _line_

    alias

    and

    begin

    Break

    case

    class

    def

    Defined?

    do

    else

    elsif

    End

    ensure

    false

    for

    If

    in

    module

    next

    Nil

    not

    or

    redo

    Rescue

    retry

    return

    self

    Super

    then

    true

    undef

    Unless

    until

    when

    while

    yield

    The syntax for storing data in a variable looks like this:


    first_name=James

    last_name=Payne

    title="Gorilla Lord"

    This tells the program that the first variable is named first_name and to store the word "James," and so on. If we wanted to use the variable to, for example, list the name and title on a screen, we could do so this way:


    puts first_name

    puts last_name

    puts title

    This would display the following text:

      James

      Payne

      "Gorilla Lord"

    I could also do this:


    puts first_name + last_name + title

    Resulting in the string: James Payne "Gorilla Lord".

    If we wanted to work with numbers and text, we could try the following.


    Age=30

    puts "I am" + String(age) + "years old!"

    age=age -8

    puts "I wish I was" +String(age) + "years old!"

    This creates a variable named Age and stores the value of 30 in it. Then it takes that value, converts it to a string (more on data types later) and adds it to our sentence. Then, it takes that value, subtracts 8 from it, and adds it to the last sentence, giving us this final result:

      "I am 30 years old!"

      "I wish I was 22 years old!"

    Interpolation of Variables

    Another way to insert the value of a variable into a string is through interpolation.


    age=30

    puts "I am #(age) years old!"

    This would display: "I am 30 years old!"

    More Ruby-on-Rails Articles
    More By James Payne


       · Before I comment, I just want to give you guys a brief heads-up: For some reason the...
       · Great article, James Payne "Gorilla Lord!" :)A couple of suggestions:1) This...
       · Hi Farsheed,Thanks for the comments. The sites are presently being redesigned,...
       · Well frankly, I have absolutely no idea how or where to begin. I'm more of a...
       · Hey! I finally found a solution to my problem regarding working in Shell and...
       · Hey Farsheed,Thanks for posting the solution. I have to agree with you regarding...
       · Hello James!As you can probably see, I finally created an account! I figured it...
     

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway