Home arrow Ruby-on-Rails arrow Page 3 - Ruby Operators and Arrays

Ruby Operators and Arrays

You've heard the phrase “diamonds are a girl's best friend”? Well, rubies are a nerd's best friend, and in this tutorial I am going to show you why. We'll discuss operators, conditionals, loops, hashes, and arrays in Ruby. And that's not all.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
October 16, 2007
  1. · Ruby Operators and Arrays
  2. · Running for Precedence
  3. · Hashes
  4. · Home on the Range

print this article

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:


blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials