Wednesday, June 28, 2006

Arrays in Ruby

An array is just a list in your computer. Every slot in the list acts like a variable: you can see what object a particular slot points to, and you can make it point to a different object. Arrays are best explained by the following example Arrays.rb.
# Arrays

# Empty array
var1 = []
# Array index starts from 0
puts var1[0]

# an array holding a single number
var2 = [5]
puts var2[0]

# an array holding two strings
var3 = ['Hello', 'Goodbye']
puts var3[0]
puts var3[2]

flavour = 'mango'
# an array whose elements are pointing
# to three objects - a float, a string and an array
var4 = [80.5, flavour, [true, false]]
puts var4[2]

name = ['Satish', 'Talim', 'Ruby', 'Java']
puts name[0]
puts name[1]
puts name[2]
puts name[3]
# the next one outputs nil
# nil is Ruby's way of saying nothing
puts name[4]
# we can add more elements too
name[4] = 'Pune'
puts name[4]
# we can add anything!
name[5] = 4.33
puts name[5]
# we can add an array to an array
name[6] = [1, 2, 3]
puts name[6]

# some methods on arrays
newarr = [45, 23, 1, 90]
puts newarr.sort
puts newarr.length

# method each (iterator) - extracts each element into lang
languages = ['Pune', 'Mumbai', 'Bangalore']

languages.each do |lang|
puts 'I love ' + lang + '!'
puts 'Don\'t you?'
end
The method each allows us to do something (whatever we want) to each object the array points to. In the example, we are able to go through each object in the array without using any numbers.


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

4 Comments:

Blogger Ashish Kulkarni said...

Dear Satish.

Does Ruby have associative arrays like awk?

What is the maximum number of entries an array can have?

Can we delete an entry in the middle and shift the remaining entries?

And can we dynamically add more entries?

I have completed the LeapYear Assignment and have uploaded it to my blog at http://ashishkulkarni.blogspot.com and request you to review it.

Regards,

Ashish.

3:56 PM  
Blogger Unknown said...

(a) Hashes (sometimes known as associative arrays, maps or dictionaries) are available in Ruby.
(b) I can't find a reference to this, but my guess is that it is restricted by your memory size.
(c) The delete method of an array can do that, for example:
a = ["a", "b", "b", "c"]
a.delete("b")
The contents of array are now "a" and "c"
(d) The insert method of the array can do that

4:20 PM  
Blogger Unknown said...

Vishal, the program when executed in SciTE, opens up a command prompt window. Previous to that you have already pressed F8 to open an output window in SciTE. Click on the Output window in SciTE and type your city and press the Enter key. Your program should work.
Can you be specific with the problem you are facing with the methods of an array?

5:00 AM  
Blogger Aspirations said...

1. What is the dimension of an array? var1 = [] I believe it's dynamic in nature? (not needed to declare the size initially)

2. If all the elements of an array need not be of same datatype, how does memory management take place?
va1 = ['anish',11, 223232.3242]...seems like they are not stored consecutive.

3. can we say var1[0] = nil?

4. var1.sort...default order of sorting? how to sort in the reverse order? In case of different datatypes, how sorting will take place?

5. Can we have two or more dimentional array? In that case what a method var1.length will return?

Anish

10:27 PM  

Post a Comment

<< Home