# Each\_with\_index

**URL:** <https://www.ruby-forum.com/t/each-with-index/196190>\
**Category:** Ruby\
**Created:** [October 8, 2010, 10:41am UTC](https://www.ruby-forum.com/t/each-with-index/196190 "2010-10-08T10:41:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 10:41am UTC](https://www.ruby-forum.com/t/each-with-index/196190/1 "2010-10-08T10:41:10Z")

</div>

hello everybody! i’m working on a programm that has that peace of code:

def show\_titles  
file = File.read(“articles\_file.txt”)  
raw\_articles = file.split("\n\n")  
raw\_articles.each do |elem|  
title, \*text = elem.split("\n")  
@array\_of\_splits \<\< Article.new(title, text.join(" "))  
end  
@array\_of\_splits.each\_with\_index do |elem, index|  
puts “#{index}: #{elem.title}”

end

the next thing i wanna do is to let the user through gets pick the title  
and then the whole article must show up in a console. For example:

1. Title 1
2. Title 2
3. Title 3

Pick the title of the article  
gets = 2

Title 2  
text 2 text 2…

The question is how can i do that using each\_with\_index ? Thank u!

---

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 11:00am UTC](https://www.ruby-forum.com/t/each-with-index/196190/2 "2010-10-08T11:00:44Z")

</div>

On Fri, Oct 8, 2010 at 10:41 AM, Pavel V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> puts “#{index}: #{elem.title}”  
> Pick the title of the article  
> gets = 2
> 
> Title 2  
> text 2 text 2…
> 
> The question is how can i do that using each\_with\_index ? Thank u!

You don’t need each\_with\_index for that. You have the list of articles  
in @array\_of\_splits, so you just use [] to get the correct article:

choice = gets.to\_i - 1 # the array starts at 0  
@array\_of\_splits[choice] # returns the Article, show the fields you want

Jesus.

---

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 11:03am UTC](https://www.ruby-forum.com/t/each-with-index/196190/3 "2010-10-08T11:03:40Z")

</div>

On Fri, Oct 8, 2010 at 10:41 AM, Pavel V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> puts “#{index}: #{elem.title}”  
> Pick the title of the article  
> gets = 2
> 
> Title 2  
> text 2 text 2…
> 
> The question is how can i do that using each\_with\_index ? Thank u!

You don’t. You just use Array#[], i.e. indexed access into the Array.

irb(main):005:0\> a=%w{foo bar}  
=\> [“foo”, “bar”]  
irb(main):006:0\> a.each\_with\_index {|e,i| printf “%2d %p\n”,i,e}  
0 “foo”  
1 “bar”  
=\> [“foo”, “bar”]  
irb(main):007:0\> a[1]  
=\> “bar”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/each-with-index/196190/4 "2010-10-08T17:24:29Z")

</div>

> You don’t need each\_with\_index for that. You have the list of articles  
> in @array\_of\_splits, so you just use [] to get the correct article:
> 
> choice = gets.to\_i - 1 # the array starts at 0  
> @array\_of\_splits[choice] # returns the Article, show the fields you want
> 
> Jesus.

Yes, but it returns me #Article:0xb78c49a4, not a title and a text!

---

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/each-with-index/196190/5 "2010-10-08T17:48:10Z")

</div>

> > You don’t need each\_with\_index for that. You have the list of articles  
> > in @array\_of\_splits, so you just use [] to get the correct article:
> > 
> > choice = gets.to\_i - 1 # the array starts at 0  
> > @array\_of\_splits[choice] # returns the Article, show the fields you want
> > 
> > Jesus.
> 
> Yes, but it returns me #Article:0xb78c49a4, not a title and a text!

The Article object should have a title and a text.  
A way to get a title is :

choice = gets.to\_i - 1 # the array starts at 0  
article = @array\_of\_splits[choice] # returns the Article, show the  
fields you want  
title = article.title

In the similar way, a text can be acquired. But we don’t know a name  
of an Article object’s method to obtain a text.  
You should know the name of the method.

Regards,

---

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/each-with-index/196190/6 "2010-10-08T17:25:56Z")

</div>

> irb(main):005:0\> a=%w{foo bar}  
> =\> [“foo”, “bar”]  
> irb(main):006:0\> a.each\_with\_index {|e,i| printf “%2d %p\n”,i,e}  
> 0 “foo”  
> 1 “bar”  
> =\> [“foo”, “bar”]  
> irb(main):007:0\> a[1]  
> =\> “bar”

Thanks

---

<div class="post-metadata">

**Author:** ![valachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/54ee81/32.png) [@valachi](https://www.ruby-forum.com/u/valachi)\
**Post date:** [October 8, 2010, 6:17pm UTC](https://www.ruby-forum.com/t/each-with-index/196190/7 "2010-10-08T18:17:58Z")

</div>

> The Article object should have a title and a text.  
> A way to get a title is :
> 
> choice = gets.to\_i - 1 # the array starts at 0  
> article = @array\_of\_splits[choice] # returns the Article, show the  
> fields you want  
> title = article.title
> 
> In the similar way, a text can be acquired. But we don’t know a name  
> of an Article object’s method to obtain a text.  
> You should know the name of the method.
> 
> Regards,

thanks, i made it up this way:

def show\_titles  
file = File.read(“articles\_file.txt”)  
raw\_articles = file.split("\n\n")  
raw\_articles.each do |elem|  
title, \*text = elem.split("\n")  
@array\_of\_splits \<\< Article.new(title, text.join("\n")) # w\o the  
whole text in one line  
end  
@array\_of\_splits.each\_with\_index do |elem, index|  
puts “#{index}: #{elem.title}”  
end  
puts  
puts “Pick the article u wanna see by entering the number of the  
title”  
choice = gets.to\_i  
puts  
puts “Title: #{@array\_of\_splits[choice].title}”  
puts “Text: #{@array\_of\_splits[choice].text}”  
end
