[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help me condence my code?

Simon Schuster

9/7/2007 12:30:00 AM

I know this could be more idiomatic to ruby.

it's basically to turn http://www.gutenberg.org/e... 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)]

21 Answers

Jeremy Woertink

9/7/2007 12:48:00 AM

0

Simon Schuster wrote:
> I know this could be more idiomatic to ruby.
>
> it's basically to turn http://www.gutenberg.org/e... 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-....

Phlip

9/7/2007 1:03:00 AM

0

Simon Schuster wrote:

> anyhow, the sloppy newbie code is as follows:

Write unit tests for it, then refactor it, one tiny change at a time,
passing all the tests after each change. If the tests fail, undo the change.

--
Phlip
http://www.oreilly.com/catalog/9780...
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax


Bil Kleb

9/7/2007 1:25:00 AM

0

Simon Schuster wrote:
> I know this could be more idiomatic to ruby.

Don't know about idiomatic, but here's my
filter-split-filter take:

require 'open-uri'

f = open('http://www.gutenberg.org/files/18362/1836...).read.
gsub(/\s*\[.*?\]/m,''). # strip comments
split("\r\n"). # DOS line endings
delete_if{ |e| e.empty? } # remove blank lines

puts f[ rand f.size ]

Regards,
--
Bil Kleb
http://nasarb.rub...

Lloyd Linklater

9/7/2007 1:56:00 AM

0

Jeremy Woertink wrote:
> end.delete_at(-1)

OMG! You can call methods in an end??? I have lived far to long in the
scalar world! When would you do such a thing?
--
Posted via http://www.ruby-....

John Mettraux

9/7/2007 2:10:00 AM

0

On 9/7/07, Lloyd Linklater <lloyd@2live4.com> wrote:
> Jeremy Woertink wrote:
> > end.delete_at(-1)
>
> OMG! You can call methods in an end??? I have lived far to long in the
> scalar world! When would you do such a thing?

irb(main):004:0> [1, 2].collect do |i| i+3; end.join " : "
=> "4 : 5"

The method call is not on the "end" but on the result of the method
using the block that the "end", well, 'closes'. Very functional.


Best regards,

--
John Mettraux -///- http://jmettraux.o...

Alex Gutteridge

9/7/2007 2:25:00 AM

0

On 7 Sep 2007, at 10:55, Lloyd Linklater wrote:

> Jeremy Woertink wrote:
>> end.delete_at(-1)
>
> OMG! You can call methods in an end??? I have lived far to long
> in the
> scalar world! When would you do such a thing?

Remember do..end is just another way of saying {..}

If I need to filter things out of an array of data I often do things
like:

data = [1,2,3,4,5,6,6,7,7]
data.find_all{|x| x > 5}.uniq
=> [6, 7]

Thats just the same as:

data.find_all do |x|
x > 5
end.uniq
=> [6, 7]

I do tend to use the {..} syntax if I'm chaining calls like this
though because 'calling' methods on end looks weird to me as well.

Alex Gutteridge

Bioinformatics Center
Kyoto University



Simon Schuster

9/7/2007 3:50:00 AM

0

putting it into phrase-test.rb and running it that way I get:

ph-test.rb:3: undefined method `delete_at' for nil:NilClass (NoMethodError)
from ph-test.rb:10:in `each_with_index'
from ph-test.rb:1:in `each'
from ph-test.rb:1:in `each_with_index'
from ph-test.rb:1

and relatedly, going through step by step I get:

023:0> end.delete_at(-1)
SyntaxError: compile error
(irb):23: syntax error, unexpected kEND
end.delete_at(-1)

I've never seen tacking things onto end before! is this possible?

> 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-....
>
>

Simon Schuster

9/7/2007 3:52:00 AM

0

eek. I've never written a unit test before. that's all incredibly
murky territory.. not only am I new to ruby, but new to programming in
general. I'll get around to it someday, I think. it just seems scary.
:)

On 9/6/07, Phlip <phlip2005@gmail.com> wrote:
> Simon Schuster wrote:
>
> > anyhow, the sloppy newbie code is as follows:
>
> Write unit tests for it, then refactor it, one tiny change at a time,
> passing all the tests after each change. If the tests fail, undo the change.
>
> --
> Phlip
> http://www.oreilly.com/catalog/9780...
> "Test Driven Ajax (on Rails)"
> assert_xpath, assert_javascript, & assert_ajax
>
>
>

Simon Schuster

9/7/2007 4:00:00 AM

0

my local copy has some extra (probably important) authorship/proj
gutenberg info at the beginning and end that I've stripped, but other
than that this code works perfectly. I really like how it's split up
by lines per method. thanks a lot!

also is \[ a literal bracket, or some kind of grouping device that
just happens to be also the boundary-character I'm looking for?


On 9/6/07, Bil Kleb <Bil.Kleb@nasa.gov> wrote:
> Simon Schuster wrote:
> > I know this could be more idiomatic to ruby.
>
> Don't know about idiomatic, but here's my
> filter-split-filter take:
>
> require 'open-uri'
>
> f = open('http://www.gutenberg.org/files/18362/1836...).read.
> gsub(/\s*\[.*?\]/m,''). # strip comments
> split("\r\n"). # DOS line endings
> delete_if{ |e| e.empty? } # remove blank lines
>
> puts f[ rand f.size ]
>
> Regards,
> --
> Bil Kleb
> http://nasarb.rub...
>
>

Alex Gutteridge

9/7/2007 4:17:00 AM

0

On 7 Sep 2007, at 09:29, Simon Schuster wrote:

> 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)]
>

The main problem is that you loop through the Array four separate
times first to chop, then to remove empty lines and then twice to
remove comments. There is no reason why you can't combine them.
Perhaps this is a good example to learn how to use 'or', 'and' and
'not':

f = []

IO.readlines("phrases_no_extra.txt").each do |l|
l.chop!
f << l if l.size != 0 and not (l.include?("]") or l.include?("["))
end

puts f[rand(f.size)]

Alex Gutteridge

Bioinformatics Center
Kyoto University