: 
http://rubylearning.com/
http://rubylearning.com/satishtalim/tutorial.html
http://rubylearning.com/download/downloads.html
http://rubylearning.com/other/certification.html
http://rubylearning.com/blog/
http://rubylearning.com/other/ruby_news.html
http://rubylearning.com/other/testimonials.html
http://rubylearning.com/jobs/ruby_jobs.html
http://rubylearning.com/other/ruby_gurus.html
http://rubylearning.com/satishtalim/services.html
http://rubylearning.com/contact/contact.html
http://rubylearning.com/satishtalim/about.html


Arrays

An Array is just a list of items in order (like mangoes, apples, and oranges). 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. You can make an array by using square brackets and is best explained by the following example p018arrays.rb. Please go through the program carefully.


# 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[1]

 

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]

 

# a trailing comma is ignored

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

 

# delete an entry in the middle and shift the remaining entries

languages.delete('Mumbai')

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. Here are a few things to remember:

  • The variable lang inside the "goalposts" refers to each item in the array as it goes through the loop. You can give this any name you want, but make it memorable.
  • The do and end identify a block of code that will be executed for each item. Blocks are used extensively in Ruby and we shall spend more time on them later.

Here's an interesting example of a method that returns an array. Example p019mtdarry.rb

def mtdarry

  10.times do |num|

    square = num * num

    return num, square if num > 5

  end

end

 

num, square = mtdarry

puts num

puts square

The output is:

>ruby mtdarry.rb

6

36

>Exit code: 0

The times method of the Integer class iterates block num times, passing in values from zero to num-1. As we can see, if you give return multiple parameters, the method returns them in an array. You can use parallel assignment to collect this return value.


Does Ruby have associative arrays like awk? Yes,  Hashes (sometimes known as associative arrays, maps or dictionaries) are available in Ruby.

 

See the complete Array documentation


Assignment:

  1. Write a Ruby program (p020arraysum.rb) that, when given an array as collection = [1, 2, 3, 4, 5] it calculates the sum of its elements.
  2. Write a Ruby program (p021oddeven.rb) that, when given an array as collection = [12, 23, 456, 123, 4579] it displays for each number, whether it is odd or even.



Learning Ruby Navigation                                                                               <Simple Constructs  | TOC  |  Ranges>