J
John Carter
I know somewhere is a collection of extensions to the enumerable module.
Is the rubygarden page
http://www.rubygarden.org/ruby?StandardClassExtensions/Enumerable
the only one?
Anyway, I have just added FirstEachLast to that.
Use it like so...
require 'FirstEachLast'
def test_first_each_last(any_enumerable_object)
result = nil
count =
any_enumerable_object.first_each_last( proc {|first|
result = "First #{first}"
},
proc {|i|
result += " then #{i},"
},
proc {|last|
result += " and last of all #{last}"
},
proc {|| result = 'Empty!'}
puts count
puts result
end
test_first_each_last( [1,2,3,4])
test_first_each_last( [])
Will result in the following output...
4
First 1 then 2, then 3, and last of all 4
0
Empty!
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
The universe is absolutely plastered with the dashed lines exactly one
space long.
Is the rubygarden page
http://www.rubygarden.org/ruby?StandardClassExtensions/Enumerable
the only one?
Anyway, I have just added FirstEachLast to that.
Use it like so...
require 'FirstEachLast'
def test_first_each_last(any_enumerable_object)
result = nil
count =
any_enumerable_object.first_each_last( proc {|first|
result = "First #{first}"
},
proc {|i|
result += " then #{i},"
},
proc {|last|
result += " and last of all #{last}"
},
proc {|| result = 'Empty!'}
puts count
puts result
end
test_first_each_last( [1,2,3,4])
test_first_each_last( [])
Will result in the following output...
4
First 1 then 2, then 3, and last of all 4
0
Empty!
John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand
The universe is absolutely plastered with the dashed lines exactly one
space long.