Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
87 lines (64 sloc) 1.93 KB
Author:
====
Niranjan Sarade
About Augmented Array:
====
The Ruby Array class is extended with some traversing/iterating methods and you will also be able to get
and set the individual array elements with indexes as of the indexes were attributes of an array.
Example:
====
require 'augmented_array'
[nil,1,2,nil,3].nilitems # => 2
[1,2,3,4,5].sum # => 15
[1,2,3,4,5,6]/1 # => [[1], [2], [3], [4], [5], [6]]
[1,2,3,4,5,6]/2 # => [[1, 2], [3, 4], [5, 6]]
[1,2,3,4,5,6]/8 # => [[1, 2, 3, 4, 5, 6]]
[]/2 # => []
[1,2,3,4,5].middle # => 3
[1,2,3,4].middle # => [2, 3]
[1].middle # => 1
ary = [1,2,3]
ary.first = 20
ary.first # => 20
[1,1.0,"test",{:key => "value"}].describe # => [{1=>"Fixnum"}, {1.0=>"Float"}, {"test"=>"String"}, {{:key=>"value"}=>"Hash"}]
ary = [1,2,3]
ary.last = 20
ary.last # => 20
[1,2,3,4,5].second_last # => 4
[1].second_last # => nil
ary = [1,2,3,4,5]
ary.alternate # => [[1, 3, 5], [2, 4]]
ary # => [1,2,3,4,5]
ary = [1,2,3,4,5]
ary.alternate! # => [[1, 3, 5], [2, 4]]
ary # => [[1, 3, 5], [2, 4]]
ary = [1,2,3,4,5,6,7]
ary.second = 20
ary.third = 30
ary.fourth = 40
ary.fifth = 50
ary # => [1, 20, 30, 40, 50]
ary.second # => 20
ary.third # => 30
ary._0 = 25
ary._0 # => 25 # Similar to ary.at(0) or ary.fetch(0)
ary._1 = 10
ary._1 # => 10
ary._11 = 11
ary # => [25, 10, 30, 40, 50, nil, nil, nil, nil, nil, nil, 11]
ary.each_index { |index|
eval("p ary._#{index}")
}
ary.each_index { |index|
eval("ary._#{index} = 10")
}
Install:
====
gem install augmented_array
(It has been pushed to http://gemcutter.org)
OR
Download the gem file from http://github.com/NiranjanSarade/augmented_array/
gem install augmented_array-0.0.1.gem
Uninstall:
====
gem uninstall augmented_array