Simon Schuster wrote:
> I know this could be more idiomatic to ruby.
> 
> it's basically to turn http://www.gutenberg.org/etext/18362 into an
> array. the "_no_extras" refers to me having snipped the intro and
> outro of the text outside of ruby. I still have to do something with
> the "SECTION" fields and the "A", "B", etc. fields. (not to mention
> some kind of linguistic parsing which would make f[rand(f.size)] + " "
> f[rand(f.size)] + " " .... link together in a coherent matter, but
> that's a little beyond me. any direction in this area would be kindly
> appreciated though! I'm thinking of separating it into different text
> files maybe. certain sections are almost whole sentences, they're
> grouped in all kinds of ways that will maybe help with this. no
> long-term goal, really, just learning ruby and having fun. :)
> 
> anyhow, the sloppy newbie code is as follows:
> 
> f = File.read("phrases_no_extra.txt")
> f = f.to_a
> f = f.each { |x| x.chop! }
> f.each_with_index { |x,y|    # deletes the empty array items
>   if x.size == 0
>     f.delete_at(y)
>   end
>   }
> f.each_with_index { |x,y|     # deleting all but the last (which is
> spread of two lines)
>   if x.include? "]"         # of his comments
>     f.delete_at(y)
>   end
>   }
> f.each_with_index { |x,y|         # yes, this is me unable to recall
> how to do "or" hahaha.
>   if x.include? "["
>     f.delete_at(y)
>   end
>   }
> f.delete_at(-1)          # random whitespace item at the end from the 
> last quote
> 
> puts f[rand(f.size)]

ok, here's my first shot at a newbie to programming trying to solve 
something like this. I hope this works for what you are trying to do. I 
wish I would have had an example to play with before posting, but oh 
well. I can only learn from this as well right? well here goes...

f = IO.readlines("phrases_no_extra.txt").each_with_index do |x, y|
  if x.chop!.size.eql?(0)
    f.delete_at(y)
  end
  if x.include?("]") or x.include?("[")
    f.delete_at(y)
  end
end.delete_at(-1)
puts f[rand(f.size)]


let me know how it goes!


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