Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Ruby Operators and Arrays
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 Operators and Arrays
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-10-16

    Table of Contents:
  • Ruby Operators and Arrays
  • Running for Precedence
  • Hashes
  • Home on the Range

  • 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 Operators and Arrays - Hashes


    (Page 3 of 4 )

    If you want to work with words instead of numbers for you indices, hashes is the way to go. Let's say you want to make a list of people's weight and print out a person from the list at random, to humiliate them throughout the day. For this, we could use Hashes, which remind me of hash browns and make me very hungry.

    Hashes are also referred to as associative arrays. And unlike arrays that use [] brackets, hashes use the curly braces {}.

     

    employees_weight = {"James" = "400", "Chuck" = "250"}

    Instead of index numbers, in Hashes we use hash keys. In the above instance, James is a hash key and 400 is the value you associate with that key. So let's say I want to embarrass that fatty James (I'm said fatty by the way), we would use the following code, then call everyone over to look at it, making sure to point and slap the cheeseburger out of his (my) hand.


    employees_weight = {"James" = "400", "Chuck" = "250"}

    puts "James weighs so much we have to put it on a line by itself:"

    puts employees_weight["James"]

    As you can see, to access the data in the hash, we used the [ ]. Why this is, I don't know. The result:

      James weighs so much we have to put it on a line by itself:

      400

    More Ruby-on-Rails Articles
    More By James Payne


       · Thanks for dropping by to read this installment of my series on Ruby. This...
     

    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