From: expandafter@... Date: 2007-09-07T17:50:04+09:00 Subject: Re: help me condence my code? On Sep 7, 3:34 am, expandaf...@yahoo.com wrote: > expandaf...@yahoo.com wrote: > > 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/18362intoan > > > 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)] > > > a = IO.read("phrases_no_extra.txt"). > > split( /[\r?\n]+/ ). > > reject{|s| > > # Reject if length is less than 2 or if it contains > > # a [ or if it's composed entirely of upper case > > # letters and spaces. > > s.size < 2 or s.index "[" or s =~ /^[A-Z ]+$/ } > > > puts a[ rand( a.size ) ] > > That contained some redundancy. > > a = IO.read("phrases_no_extra.txt"). > split( /[\r?\n]+/ ). > reject{|s| > # Reject if it contains a [ or if > # it's composed entirely of upper case > # letters and spaces. > s.index "[" or s =~ /^[A-Z ]+$/ } > > puts a[ rand( a.size ) ] I wish that I were awake. That regular expression wasn't very sensible. a = IO.read("phrases_no_extra.txt"). split( /(?:\r?\n)+/ ). reject{|s| # Reject if it contains a [ or if # it's composed entirely of upper case # letters and spaces. s.index "[" or s =~ /^[A-Z ]+$/ } puts a[ rand( a.size ) ]