Each_with_index

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!

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On Fri, Oct 8, 2010 at 10:41 AM, Pavel Varela <impulse221@yandex.ru> 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. 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

···

On Fri, Oct 8, 2010 at 10:41 AM, Pavel Varela <impulse221@yandex.ru> 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!

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

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!

···

--
Posted via http://www.ruby-forum.com/\.

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

···

--
Posted via http://www.ruby-forum.com/\.

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,

···

--
NOBUOKA Yuya

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

···

--
Posted via http://www.ruby-forum.com/\.