



(4 ratings)
You are already familiar with a couple of Ruby classes (Integer and String). The class Array is used to represent a collection of items.
This is best seen through an example:
$ irb --simple-prompt |
Here the class method tells us that the variable numbers is an Array. You can access the individual elements of the array like this:
>> numbers[0] |
You can add more entries to the array by simply typing:
>> numbers[5] = "five" |
Notice that the entries in the array are stored sequentially, starting at 0. An array can contain any number of objects. The objects contained in the array can be manipulated just as before.
>> numbers[3].class |
| Warning: Notice that Array's start counting at 0, not 1 |
What kind of things can you put on arrays? Well, any object really. How about strings and integers?:
>> address = [ 284, "Silver Spring Rd" ] |
How about another array?
>> addresses = [ [ 284, "Silver Sprint Rd" ], [ 344, "Ontario Dr" ] ] |
Can you see why arrays are so cool?
20 Random Tutorials from the same category :
Sorting the addressbook RUBY
Implementing AddressBook In RUBY
Writing iterators In RUBY
Classes and methods in RUBY
Progress Bars with GD2 and Ruby
What can ruby arrays do?
Iterators In RUBY
The Scalability of Ruby
Example: Addressbook RUBY
Printing the addressbook RUBY
Difference of Gtk+ and Ruby/Gtk
More features IN RUBY
Arrays In RUBY
Hashes In RUBY













