Ruby/Arrays Hashes Ranges2

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application
Arrays.Hashes.Ranges page2


The first elements of the array:

toppings.first

The last element of the array:

toppings.last

To check if a data structure is an array:

puts toppings.type

or even better, use the class method since "type" is considered deprecated and will give you a warning.

puts toppings.class

Image:Array.gif

An array does not have to include only data of a single type.You can can mix chars,strings or numbers as you please. Like this:

cool = ['i', 'see', 'u','!', 10]

Image:Array2.gif

Hashes

As arrays, Hashes are used for storing data also.Hashes map an object to another object, think of assigning a string a certain "meaning" in your program; and each time you refer that string, you actually refer to it's meaning.

How to create an empty hash:

same as when creating an empty array: using the "new" method:

pizza = Hash.new

Or by using the curly brackets:

pizza = {
'marguerita' => 'tomato sauce and cheese',
'quattro formaggi' => 'mozzarella,gorgonzola,parmigiano and ricotta',
'quattro stagioni' => 'prosciutto, mozzarella, parmigiano,mushrooms,clams'
}

Image:Hash.gif

This is how you find out to what each "key" in the hash is associated with:

pizza['marguerita']

Image:Hash2.gif

Previous Next
Personal tools