0% found this document useful (0 votes)
6 views24 pages

Array

Ruby Array

Uploaded by

King_Jangid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Array

Ruby Array

Uploaded by

King_Jangid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd

Chapter 6: Arrays

By:
Kushal Jangid

Introduction
The Array class is one of Rubys built-in classes. Arrays
are compact, ordered collections of objects. Ruby arrays
can hold objects such as String , Integer , Fixnum , Hash ,
Symbol , even other Array objectsyou name it. Any
object that Ruby can create, it can hold in an array.

Creating Arrays
months = [Link]
This creates an empty array, represented as [] , named months.
Test for Empty Array :
[Link]? # => true
Set the size of an array :
months = [Link](12)
months = [Link] 12
[Link] # => 12 OR [Link] # => 12

Array Operations
To inspect the arraythat is, to look at the array as an array use:
puts [Link]
p months
Output : [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
month = [Link](12, "month")
["month", "month", "month", "month", "month", "month", "month",
"month", "month", "month", "month", "month"]

Clear the Deck

[Link] # => []
[Link]? # => true

Creating an Array with a Block


num = [Link](10) { |e| e = e * 2 }
giving you an array like this: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Another easy way :


months = [ nil, "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" ]

Iterate over Array


hodge_podge = ["January", 1, :year, [2006,01,01]]
hodge_podge.each {|e| print [Link], " " }
# => String Fixnum Symbol Array

Accessing Elements
q1 = [ January, February, March]

q1[0] # => January


q1[1] # => February
[Link](0) # => January
You can access the last element in the array with:
q1[-1] # => March
[Link] # => January
[Link] # => March

Accessing Arrays
year = [2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]
year[0, 3] # => [2000, 2001, 2002]
year[7..9] # => [2007, 2008, 2009]
Year[7...9] # => [2007, 2008]
Instead of [] , you can also use the slice method, another alias:
[Link](1) # => 2001
[Link](0,4) # => [2000, 2001, 2002, 2003]
[Link](0..2) # => [2000, 2001, 2002]
[Link](0...2) # => [2000, 2001]

Concatenation of Arrays
q1
q2
q3
q4

= [ January, February, March ]


= [April , May ,June ]
= [ July, August, September ]
= [ October,November, December ]

half1 = q1 + q2
half2 = q3 + q4
yr = half1 + half2
["January", "February", "March", "April", "May", "June",
"July", "August","September", "October", "November",
"December"]

Set Operations
Ruby can do several set operations on arrays, such as:
Intersection with &
Difference with Union with |

Set Operations
tue = [ "shop", "make pie", "sleep" ]
wed = [ "shop", "make pie", "read", "sleep" ]
tue & wed # => ["shop", "make pie", "sleep"]
Difference ( - ) creates a new array, removing elements that
appear in both arrays:
wed - tue # => ["read"]
Union ( | ) joins two arrays together, removing duplicates:
tue | wed # => ["shop", "make pie", "read", "sleep"]

Unique Elements
shopping_list =[cheese, bread, crackers, potatoes, cheese ]
Find unique elements in the array :
shopping_list.uniq!=> ["cheese", "bread", "crackers", "potatoes"]

Blow Your Stack


A stack is a LIFO (last in, first out) structure. You can use an array like a
stack by using the push and pop methods from the Array class. Heres
how :
fruit = [apple, orange, banana]
[Link] # => "banana"
p fruit # => ["apple", "orange" ]
[Link] "mango"
p fruit # => ["apple", "orange", "mango"]

Comparing Arrays
Three methods allow you to compare arrays to see if they are equal.
Those methods are == , <=> , and eql? .

bob = [ "full", 40, "yes" ]


lou = ["part", 23, "no"]
schlomo = [ "full", 40, "yes" ]
lou == lou # => true
bob == schlomo # => true
schlomo == lou # => false

Whats the difference between == and


eql? . eql? checks to see if the values
are equal (as in == ), but also checks if
the values are of the same type.
Another way to compare arrays is with <=>
(spaceship operator). When it compares two
arrays, it compares each object in the arrays.
The two arrays are considered equal if they
are the same length and if the value of each
element is equal to the value of the
corresponding element in the other array.

Changing Elements
months = ["nil", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]

months[5..7] = "Mai", "Juni", "Juli"

# => [nil, "January", "February", "March", "April", "Mai", "Juni", "Juli",


"August", "September", "October", "November", "December"]

Array As a String
greeting = [ "Hello! ", "Bonjour! ", "Guten Tag!" ]
puts greeting.to_s # => Hello! Bonjour! Guten Tag!

Using shift and unshift


Another way to remove an element from an array is with the shift
method. This method returns the first element of an array ( nil if the
array is empty), and then removes the element, shifting all other elements
down by one.

dates = [ 4, 5, 6, 7 ] # => [4, 5, 6, 7]


[Link] # => 4
p dates # => [5, 6, 7]
[Link](2,3) # => [2, 3, 4, 5, 6, 7]

Deleting Elements
months = ["nil", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug",
"sep", "oct", "nov", "dec"]
month_a.delete_at( 12 ) # => "dec"
p month_a # ["nil", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug",
"sep", "oct", "nov"]

Arrays and Blocks


Array has an each method, toojust like lots of other Ruby
classes. each lets you iterate over every element in an array and
do something to it.

month_a.each { |e| print [Link] + " " }


Output : Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
The map method (and its synonym collect ) is similar to each ,
but it returns a new array instead of a string.
month_a_2007 = month_a.map { |e| [Link] + " 2007" }

Sorting Things and About Face


x = [ 2, 5, 1, 7, 23, 99, 14, 27 ]
Sorting x:
[Link]! # => [1, 2, 5, 7, 14, 23, 27, 99]

Multidimensional Arrays
A multidimensional array is an array of arrays. You create such
an array by giving array elements that are themselves arrays.
This is a two-dimensional array:
d2 = [ ["January", 2007],
["February", 2007],
["March", 2007] ]
Lets turn d2 into a one-dimensional array with flatten .
[Link]
# => ["January", 2007, "February", 2007, "March", 2007]

Multidimensional Arrays
A two-dimensional array is like a table, with rows and columns. Lets
try the transpose method on d2 , turning this:

d2 = [ ["January", 2007], ["February", 2007], ["March", 2007] ]


into this:
[Link]
# => => [["January", "February", "March"], [2007, 2007, 2007]]

Thank You

You might also like