Hash name increment on iteration?

S

Shaz

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]...
 
R

Robert Klemme

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
 
B

Brian Candler

Is this what you want?

SomeArray.each_with_index do |(name,content),i|
...
end
 
S

Shaz

Is this what you want?

SomeArray.each_with_index do |(name,content),i|
  ...
end

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

Brian Candler

Shaz said:
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
end
end
end
 
L

Li Chen

Brian said:
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
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
 
J

Jesús Gabriel y Galán

Brian said:
Inside it will be implemented something like this:

module Enumerable
=A0 def each_with_index
=A0 =A0 i =3D 0
=A0 =A0 each do |elem|
=A0 =A0 =A0 yield elem, i
=A0 =A0 =A0 i +=3D 1
=A0 =A0 end
=A0 end
end

But you could consider it like this:

class Array
=A0 def each_with_index
=A0 =A0 size.times do |i|
=A0 =A0 =A0 yield self, i
=A0 =A0 end
=A0 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"?


When there's no explicit receiver, the method call happens on the
object that is "self" at that point.

Jesus.
 
L

Li Chen

Jesús Gabriel y Galán said:
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
 
C

Caleb Clausen

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top