From: "Jesús Gabriel y Galán" Date: 2010-10-08T17:57:21+09:00 Subject: Re: each_with_index On Fri, Oct 8, 2010 at 10:41 AM, Pavel Varela wrote: > 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! 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.