# Each\_with\_index

**URL:** <https://www.ruby-forum.com/t/each-with-index/179168>\
**Category:** Ruby\
**Created:** [November 18, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/each-with-index/179168 "2009-11-18T21:56:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 18, 2009, 9:56pm UTC](https://www.ruby-forum.com/t/each-with-index/179168/1 "2009-11-18T21:56:54Z")

</div>

Hi, I’m trying to get my file to take in a list of names (a firstname,  
surname and middlename) and to then list each part of the name with an  
index. For example one name “John, Smith, Paul” should be printed as  
1.John  
2.Smith  
3.Paul

I can list the index as 1.John S. Paul but am unsure on how to list  
each part of the name seperately, does anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 18, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/each-with-index/179168/2 "2009-11-18T22:45:26Z")

</div>

Jane O’Maley wrote:

> Hi, I’m trying to get my file to take in a list of names (a firstname,  
> surname and middlename) and to then list each part of the name with an  
> index. For example one name “John, Smith, Paul” should be printed as  
> 1.John  
> 2.Smith  
> 3.Paul
> 
> I can list the index as 1.John S. Paul but am unsure on how to list  
> each part of the name seperately, does anyone have any ideas?

Create one array with all the strings in it. Or define Person#each or  
Person#to\_a.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ladyinred](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/3e96dc/32.png) [@ladyinred](https://www.ruby-forum.com/u/ladyinred)\
**Post date:** [November 18, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/each-with-index/179168/3 "2009-11-18T22:47:43Z")

</div>

Jane O’Maley wrote:

> Hi, I’m trying to get my file to take in a list of names (a firstname,  
> surname and middlename) and to then list each part of the name with an  
> index. For example one name “John, Smith, Paul” should be printed as  
> 1.John  
> 2.Smith  
> 3.Paul
> 
> I can list the index as 1.John S. Paul but am unsure on how to list  
> each part of the name seperately, does anyone have any ideas?

“John, Smith, Paul”.split(", ").each\_with\_index do |name, index|  
puts “#{index + 1}. #{name}\n”  
end

~Jeremy W.
