I've found myself writing the following code to address needs I have
processing formatted text files, but is this possible some other and I
am going through foolish gyrations here...
Here is the code:
class Array
alias :orig_each :each
def each(cnt = 1, &blk)
if cnt == 1
orig_each { |x| blk.call(x) }
else
0.step(self.size - 1,cnt) do |i|
yield(self.slice(i,cnt))
end
end
end
end
so I can do things like this
arr = [ key, value, value, key, value, value, key, value, value, ... ]
arr.each(3) do |key,val1,val2|
#manipulate them as I see fit
end
Thanks for any help you can give!
Charles Churchill
Account Executive
Datatek, Inc.
800 536-4835 ext. 145
1 919 425-3145 (international)
churchill@datatek-net.com
Have you looked at Enumerable#each_slice which is included in Array?
ratdog:~ mike$ ri Enumerable#each_slice
-------------------------------------------------- Enumerable#each_slice
e.each_slice(n) {...}
···
On 11-May-06, at 10:53 AM, Charles Churchill wrote:
I've found myself writing the following code to address needs I have
processing formatted text files, but is this possible some other and I
am going through foolish gyrations here...
Here is the code:
class Array
alias :orig_each :each
def each(cnt = 1, &blk)
if cnt == 1
orig_each { |x| blk.call(x) }
else
0.step(self.size - 1,cnt) do |i|
yield(self.slice(i,cnt))
end
end
end
end
so I can do things like this
arr = [ key, value, value, key, value, value, key, value, value, ... ]
arr.each(3) do |key,val1,val2|
#manipulate them as I see fit
end
Thanks for any help you can give!
------------------------------------------------------------------------
Iterates the given block for each slice of <n> elements.
e.g.:
(1..10).each_slice(3) {|a| p a}
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
Mike
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
require 'enumerator'
(1..9).to_enum.each_slice(3) { |x, y, z| puts "%i %i %i" % [x, y, z] }
1 2 3
4 5 6
7 8 9
-Alder
···
On 5/11/06, Charles Churchill <churchill@datatek-net.com> wrote:
I've found myself writing the following code to address needs I have
processing formatted text files, but is this possible some other and I
am going through foolish gyrations here...
Here is the code:
class Array
alias :orig_each :each
def each(cnt = 1, &blk)
if cnt == 1
orig_each { |x| blk.call(x) }
else
0.step(self.size - 1,cnt) do |i|
yield(self.slice(i,cnt))
end
end
end
end
so I can do things like this
arr = [ key, value, value, key, value, value, key, value, value, ... ]
arr.each(3) do |key,val1,val2|
#manipulate them as I see fit
end
Thanks for any help you can give!
Charles Churchill
Account Executive
Datatek, Inc.
800 536-4835 ext. 145
1 919 425-3145 (international)
churchill@datatek-net.com
Mike Stok wrote:
I've found myself writing the following code to address needs I have
processing formatted text files, but is this possible some other and I
am going through foolish gyrations here...
Here is the code:
class Array
alias :orig_each :each
def each(cnt = 1, &blk)
if cnt == 1
orig_each { |x| blk.call(x) }
else
0.step(self.size - 1,cnt) do |i|
yield(self.slice(i,cnt))
end
end
end
end
so I can do things like this
arr = [ key, value, value, key, value, value, key, value, value, ... ]
arr.each(3) do |key,val1,val2|
#manipulate them as I see fit
end
Thanks for any help you can give!
Have you looked at Enumerable#each_slice which is included in Array?
ratdog:~ mike$ ri Enumerable#each_slice
-------------------------------------------------- Enumerable#each_slice
e.each_slice(n) {...}
------------------------------------------------------------------------
Iterates the given block for each slice of <n> elements.
e.g.:
(1..10).each_slice(3) {|a| p a}
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
Mike
exacly what I was looking for. I had checked the ruby docs, but only
looked under Array.... 
Sorry for the apparently stupid question.
Thanks,
Charles Churchill
Account Executive
Datatek, Inc.
800 536-4835 ext. 145
1 919 425-3145 (international)
churchill@datatek-net.com
···
On 11-May-06, at 10:53 AM, Charles Churchill wrote:
each_slice doesn't wfm if I don't require 'enumerator' first:
irb(main):001:0> (1..10).each_slice(3) {|a| p a}
(1..10).each_slice(3) {|a| p a}
NoMethodError: undefined method `each_slice' for 1..10:Range
from (irb):1
irb(main):002:0> require 'enumerator'
require 'enumerator'
true
irb(main):003:0> (1..10).each_slice(3) {|a| p a}
(1..10).each_slice(3) {|a| p a}
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
nil
···
On 5/11/06, Mike Stok <mike@stok.ca> wrote:
On 11-May-06, at 10:53 AM, Charles Churchill wrote:
> I've found myself writing the following code to address needs I have
> processing formatted text files, but is this possible some other and I
> am going through foolish gyrations here...
>
> Here is the code:
>
> class Array
> alias :orig_each :each
> def each(cnt = 1, &blk)
> if cnt == 1
> orig_each { |x| blk.call(x) }
> else
> 0.step(self.size - 1,cnt) do |i|
> yield(self.slice(i,cnt))
> end
> end
> end
> end
>
> so I can do things like this
>
> arr = [ key, value, value, key, value, value, key, value, value, ... ]
> arr.each(3) do |key,val1,val2|
> #manipulate them as I see fit
> end
>
> Thanks for any help you can give!
Have you looked at Enumerable#each_slice which is included in Array?
ratdog:~ mike$ ri Enumerable#each_slice
-------------------------------------------------- Enumerable#each_slice
e.each_slice(n) {...}
------------------------------------------------------------------------
Iterates the given block for each slice of <n> elements.
e.g.:
(1..10).each_slice(3) {|a| p a}
# outputs below
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10]
Mike
--
Mike Stok <mike@stok.ca>
Mike Stok
The "`Stok' disclaimers" apply.
Of course it would help if the answer was correct - see Alder Green's response.
I should have tried it before posting!
Mike
···
On 11-May-06, at 11:09 AM, Charles Churchill wrote:
exacly what I was looking for. I had checked the ruby docs, but only
looked under Array.... 
Sorry for the apparently stupid question.
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
You can as well use direct assignment, say if you want just every
third element you can do:
17:32:15 [~]: ruby -r enumerator -e '(1..10).each_slice(3) {|a,| p a}'
1
4
7
10
Kind regards
robert
cool, didn't know about that one!
-Alder
···
On 5/11/06, Robert Klemme <shortcutter@googlemail.com> wrote:
You can as well use direct assignment, say if you want just every
third element you can do:
17:32:15 [~]: ruby -r enumerator -e '(1..10).each_slice(3) {|a,| p a}'
1
4
7
10
Kind regards
robert