# Hash name increment on iteration?

**URL:** <https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508>\
**Category:** Ruby\
**Created:** [April 8, 2010, 3:30am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508 "2010-04-08T03:30:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 8, 2010, 3:30am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/1 "2010-04-08T03:30:54Z")

</div>

Is it possible to add to the name of a hash while running through an  
iteration? What I’m trying to achieve:

SomeArray.each do |i, name, content|  
info[i+1] = { “name” =\> name, “content” =\> content }  
end

So that I can call, for example info[1] [name] or info[5][content]…

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 8, 2010, 9:15am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/2 "2010-04-08T09:15:41Z")

</div>

On 08.04.2010 03:27, Shaz wrote:

> Is it possible to add to the name of a hash while running through an  
> iteration? What I’m trying to achieve:
> 
> SomeArray.each do |i, name, content|  
> info[i+1] = { “name” =\> name, “content” =\> content }  
> end
> 
> So that I can call, for example info[1] [name] or info[5][content]…

Make info an Array and just use

info \<\< { :name =\> name, :content =\> content }

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 8, 2010, 9:44am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/3 "2010-04-08T09:44:08Z")

</div>

Is this what you want?

SomeArray.each\_with\_index do |(name,content),i|  
…  
end

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 11, 2010, 9:30am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/4 "2010-04-11T09:30:48Z")

</div>

Shaz wrote:

> Each with index worked for me - didn’t realize each variable in the  
> array has an index that can be called.

Not exactly. The ‘each\_with\_index’ method just yields the array index to  
the block being called.

Inside it will be implemented something like this:

module Enumerable  
def each\_with\_index  
i = 0  
each do |elem|  
yield elem, i  
i += 1  
end  
end  
end

But you could consider it like this:

class Array  
def each\_with\_index  
size.times do |i|  
yield self[i], i  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 11, 2010, 2:40am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/5 "2010-04-11T02:40:58Z")

</div>

On 8 Apr, 08:44, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this what you want?
> 
> SomeArray.each\_with\_index do |(name,content),i|  
> …  
> end
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Each with index worked for me - didn’t realize each variable in the  
array has an index that can be called. Thanks!

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 11, 2010, 10:12pm UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/6 "2010-04-11T22:12:11Z")

</div>

On Sun, Apr 11, 2010 at 5:32 PM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > end
> > 
> > ```
> > 
> > end
> 
> 1. which one is the reciever for “each”?
> 2. which one is the reciever for “size”?

When there’s no explicit receiver, the method call happens on the  
object that is “self” at that point.

Jesus.

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 12, 2010, 12:34am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/7 "2010-04-12T00:34:20Z")

</div>

JesÃºs Gabriel y GalÃ¡n wrote:

> On Sun, Apr 11, 2010 at 5:32 PM, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > ï¿½ ï¿½ end  
> > > ï¿½ end
> > 
> > 1. which one is the reciever for “each”?
> > 2. which one is the reciever for “size”?
> 
> When there’s no explicit receiver, the method call happens on the  
> object that is “self” at that point.
> 
> Jesus.

Hi Gabriel,

Where can I find more info about method call on implicit receiver?

Li

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 12, 2010, 5:21am UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/8 "2010-04-12T05:21:47Z")

</div>

On 4/11/10, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Where can I find more info about method call on implicit receiver?

Seems like we’ve had a number of newbies who’ve been perplexed about  
this recently. Maybe this should be in the faq. Isn’t there a faq  
somewhere?

---

<div class="post-metadata">

**Author:** ![shaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@shaz](https://www.ruby-forum.com/u/shaz)\
**Post date:** [April 11, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/hash-name-increment-on-iteration/186508/9 "2010-04-11T17:32:38Z")

</div>

Brian C. wrote:

> Inside it will be implemented something like this:
> 
> module Enumerable  
> def each\_with\_index  
> i = 0  
> each do |elem|  
> yield elem, i  
> i += 1  
> end  
> end  
> end
> 
> But you could consider it like this:
> 
> class Array  
> def each\_with\_index  
> size.times do |i|  
> yield self[i], i  
> end  
> end  
> end

Hi Brian,

Just some follow-up questions for these codes:  
I recall the the method call in Ruby follow this format:  
“receiver.method”  
So

1. which one is the reciever for “each”?
2. which one is the reciever for “size”?

Thanks,

Li
