each_with_index

P

Pavel Varela

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!
 
J

Jesús Gabriel y Galán

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

=A0def show_titles
=A0 =A0file =3D File.read("articles_file.txt")
=A0 =A0raw_articles =3D file.split("\n\n")
=A0 =A0raw_articles.each do |elem|
=A0 =A0title, *text =3D elem.split("\n")
=A0 =A0@array_of_splits << Article.new(title, text.join(" "))
=A0 =A0end
=A0 =A0@array_of_splits.each_with_index do |elem, index|
=A0 =A0puts "#{index}: #{elem.title}"

=A0end

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 =3D 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 =3D gets.to_i - 1 # the array starts at 0
@array_of_splits[choice] # returns the Article, show the fields you want

Jesus.
 
R

Robert Klemme

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

=A0def show_titles
=A0 =A0file =3D File.read("articles_file.txt")
=A0 =A0raw_articles =3D file.split("\n\n")
=A0 =A0raw_articles.each do |elem|
=A0 =A0title, *text =3D elem.split("\n")
=A0 =A0@array_of_splits << Article.new(title, text.join(" "))
=A0 =A0end
=A0 =A0@array_of_splits.each_with_index do |elem, index|
=A0 =A0puts "#{index}: #{elem.title}"

=A0end

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 =3D 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=3D%w{foo bar}
=3D> ["foo", "bar"]
irb(main):006:0> a.each_with_index {|e,i| printf "%2d %p\n",i,e}
0 "foo"
1 "bar"
=3D> ["foo", "bar"]
irb(main):007:0> a[1]
=3D> "bar"

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top