# Each with arguments

**URL:** <https://www.ruby-forum.com/t/each-with-arguments/60249>\
**Category:** Ruby\
**Created:** [May 11, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249 "2006-05-11T16:57:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/1 "2006-05-11T16:57:20Z")

</div>

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 C.  
Account Executive  
Datatek, Inc.  
800 536-4835 ext. 145  
1 919 425-3145 (international)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/2 "2006-05-11T17:06:49Z")

</div>

On 11-May-06, at 10:53 AM, Charles C. wrote:

> ```
> orig_each { |x| blk.call(x) }
> 
> ```
> 
> 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 S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/3 "2006-05-11T17:06:55Z")

</div>

On 5/11/06, Charles C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> orig_each { |x| blk.call(x) }
> 
> ```
> 
> arr = [key, value, value, key, value, value, key, value, value, …]  
> 1 919 425-3145 (international)  
> removed\_email\_address@domain.invalid

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

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/4 "2006-05-11T17:10:28Z")

</div>

Mike S. wrote:

> > ```
> > alias :orig_each :each
> > 
> > ```
> 
> ```
> [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 C.  
Account Executive  
Datatek, Inc.  
800 536-4835 ext. 145  
1 919 425-3145 (international)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/5 "2006-05-11T17:13:37Z")

</div>

On 5/11/06, Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > alias :orig_each :each
> > 
> > ```
> 
> ```
> [1, 2, 3]
> 
> ```
> 
> The “`Stok’ disclaimers” apply.

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

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 5:35pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/6 "2006-05-11T17:35:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/7 "2006-05-11T18:36:15Z")

</div>

On 5/11/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> robert

cool, didn’t know about that one!

-Alder

---

<div class="post-metadata">

**Author:** ![cmcfab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/838e76/32.png) [@cmcfab](https://www.ruby-forum.com/u/cmcfab)\
**Post date:** [May 11, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/each-with-arguments/60249/8 "2006-05-11T17:20:27Z")

</div>

On 11-May-06, at 11:09 AM, Charles C. wrote:

> exacly what I was looking for. I had checked the ruby docs, but only  
> looked under Array… ☹
> 
> Sorry for the apparently stupid question.

Of course it would help if the answer was correct - see Alder G.'s  
response.

I should have tried it before posting!

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.
