Ruby/Arrays Hashes Ranges

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

[edit] Ruby Arrays.Hashes.Ranges

Arrays are used to store collections of data objects.If this sounds too fancy for you, try imagining a situation like this:

You want to order pizza online from your local pizzeria which just happens to also offer an online ordering application. They offer standard pizzas but they also allow customers to order their custom made pizza by offering them the possibility of choosing the type of dough used and the toppings.

How do you think the application "remembers" which toppings are available to customers? They're probably using an array, that's the answer. Something like this:

toppings = [onions,mushrooms,pineapple,anchovies,salami,gorgonzola]

Let's dissect this example now and learn how arrays are used at the same time:

Each object in the array above has an unique key assigned to it.This is very useful when you want to address only one of the elements included in the array.The first element of the array is at position 0, so this means that toppings[0] will return "onions"; toppings[1] will return "mushrooms" and so on.

If you ever need just to create an empty array and add data to it later, you use the "new" method:

toppings = Array.new

If later one you want to assign elements to the array, you would do it like this:

toppings[0] = "onions"
toppings[1] = "mushrooms"

What if you wanted to create an array from a list of give strings? You can do it very easily like this:

toppings = %w[onions mushrooms pineapple anchovies salami gorgonzola]

Once that done, you simply reference an element by using it's index key:

toppings[4]

Here's a list of methods that you can use with arrays to test for different situations:

Test if the array is empty:

toppings.empty?

See how many elements the array contains:

toppings.size

or

puts toppings.length

Delete an element from an array:

toppings.delete "salami"


Previous Next
Personal tools