Ruby Array


Basics of ruby Array.

1.How to create ruby array

1.9.2p318 :002 > order = [“Book”,”Pen”,”Computer”,”Bus”]

=> [“Book”, “Pen”, “Computer”, “Bus”]

1.9.2p318 :002 > list = %(car moter rails ruby)

=> “car moter rails ruby”

1.9.2p318 :003 > name = Array.new

=> []

1.9.2p318 :004 > name << ‘Pravin’

=> [“Pravin”]

1.9.2p318 :005 > name << ‘Ankit’

=> [“Pravin”, “Ankit”]

1.9.2p318 :006 > name << ‘Abhay’

=> [“Pravin”, “Ankit”, “Abhay”]

1.9.2p318 :007 > name << ‘Abinaw’

=> [“Pravin”, “Ankit”, “Abhay”, “Abinaw”]

2. collect and map

Invokes block once for each element of self. Creates a new array containing the values returned by the block

1.9.2p318 :030 > order.collect{|item| item}

=> [“Book”, “Pen”, “Computer”, “Bus”]

1.9.2p318 :031 > order.collect{|item| item+”1″}

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :032 > order

=> [“Book”, “Pen”, “Computer”, “Bus”]

1.9.2p318 :033 > order.map{|item| item+”1″}

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :034 > order

=> [“Book”, “Pen”, “Computer”, “Bus”]

3. collect! And map!

Invokes the block once for each element of self, replacing the element with the value returned by block

1.9.2p318 :035 > order.map!{|item| item+”1″}

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :036 > order

=> [“Book1”, “Pen1”, “Computer1”, “Bus1”]

1.9.2p318 :037 > order.collect!{|item| item+”2″}

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

1.9.2p318 :038 > order

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

4.compact

Returns a copy of self with all nil elements removed.

1.9.2p318 :050 > order

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

push a nil element to array

1.9.2p318 :059 > order << nil

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”, nil]

1.9.2p318 :060 > order.compact

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”, “”, “”, “nil”]

5.compact!

Removes nil elements from array. Returns nil if no changes were made.

1.9.2p318 :062 > order << nil

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”, nil]

1.9.2p318 :063 > order.compact!

=> [“Book12”, “Pen12”, “Computer12”, “Bus12”]

1.9.2p318 :064 > order.compact!

=> nil

6.delete_if

Deletes every element of self for which block evaluates to true.

1.9.2p318 :088 > number_list = [‘2′,’8′,’4′,’3′,’9’]

=> [“2”, “8”, “4”, “3”, “9”]

1.9.2p318 :089 > number_list.delete_if{|item| item >= ‘4’}

=> [“2”, “3”]

7.empty?

Returns true if self array contains no elements.

1.9.2p318 :090 > number_list

=> [“2”, “3”]

1.9.2p318 :091 > number_list.empty?

=> false

1.9.2p318 :092 > number_list.delete_if{|item| item > ‘0’}

=> []

1.9.2p318 :093 > number_list

=> []

1.9.2p318 :094 > number_list.empty?

=> true

8.eql?

Returns true if array and other are the same object, or are both arrays with the same content.

1.9.2p318 :097 > number_list << ‘4’

=> [“4”]

1.9.2p318 :098 > number_list << 8

=> [“4”, 8]

1.9.2p318 :099 > number_list << “test”

=> [“4”, 8, “test”]

1.9.2p318 :101 > number_list1 = Array.new

=> []

1.9.2p318 :102 > number_list1 << “test”

=> [“test”]

1.9.2p318 :103 > number_list1 << 5

=> [“test”, 5]

1.9.2p318 :104 > number_list1.eql?(number_list)

=> false

1.9.2p318 :105 > number_list1

=> [“test”, 5]

1.9.2p318 :106 > number_list1.delete_at(1)

=> 5

1.9.2p318 :107 > number_list1

=> [“test”]

1.9.2p318 :108 > number_list

=> [“4”, 8, “test”]

1.9.2p318 :109 > number_list.delete_at(1)

=> 8

1.9.2p318 :110 > number_list.delete_at(0)

=> “4”

1.9.2p318 :111 > number_list

=> [“test”]

1.9.2p318 :112 > number_list1.eql?(number_list)

=> true

9.include?

Returns true if the given object is present in self (that is, if any object == anObject), false otherwise.

1.9.2p318 :124 > number_list

=> [“test”]

1.9.2p318 :125 > number_list.push(5)

=> [“test”, 5]

1.9.2p318 :126 > number_list.push(“apple”)

=> [“test”, 5, “apple”]

1.9.2p318 :127 > number_list.include?(5)

=> true

1.9.2p318 :128 > number_list.include?(‘5’)

=> false

1.9.2p318 :129 > number_list.include?(‘apple’)

=> true

1.9.2p318 :130 > number_list.include?(‘apple1’)

=> false

10.index(obj)

Returns the index of the first object in self such that is == to obj. Returns nil if no match is found.

1.9.2p318 :141 > number_list

=> [“test”, 5, “apple”]

1.9.2p318 :142 > number_list.index(5)

=> 1

1.9.2p318 :143 > number_list.index(‘test’)

=> 0

1.9.2p318 :144 > number_list.index(’empty’)

=> nil

11.join

Returns a string created by converting each element of the array to a string, separated by sep.

1.9.2p318 :155 > number_list

=> [“test”, 5, “apple”]

1.9.2p318 :157 > number_list.join(‘-‘)

=> “test-5-apple”

12.nitems

Returns the number of non-nil elements in self. May be zero.

13. slice!

Deletes the element(s) given by an index (optionally with a length) or by a range. Returns the deleted object, subarray, or nil if the index is out of range. Equivalent to:

1.9.2p318 :220 > number_list

=> [“test”, 5, “dog”]

1.9.2p318 :221 > number_list.slice!(2)

=> “dog”

1.9.2p318 :222 > number_list

=> [“test”, 5]

1.9.2p318 :223 > number_list.slice!(2)

=> nil

1.9.2p318 :224 > number_list

=> [“test”, 5]

1.9.2p318 :225 > number_list.slice!(0)

=> “test”

1.9.2p318 :226 > number_list

=> [5]

14.sort

Returns a new array created by sorting self. Comparisons for the sort will be done using the <=> operator or using an optional code block. The block implements a comparison between a and b, returning -1, 0, or +1.

1.9.2p318 :229 > number = [‘2′,’9′,’7′,’4’]

=> [“2”, “9”, “7”, “4”]

1.9.2p318 :230 > number.sort{|a,b| a <=> b}

=> [“2”, “4”, “7”, “9”]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s