From: William James Date: 2007-09-08T00:25:07+09:00 Subject: Re: help me condence my code? On Sep 6, 7:29 pm, "Simon Schuster" wrote: > I know this could be more idiomatic to ruby. > > it's basically to turnhttp://www.gutenberg.org/etext/18362into 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)] puts IO.readlines("phrases_no_extra.txt"). reject{|x| x =~ /\[|^[A-Z ]*$/}. instance_eval{ self[ rand(self.size) ] }