Robert Dober
6/30/2007 2:37:00 PM
On 6/30/07, Ian Whitlock <iw1junk@comcast.net> wrote:
> Robert Dober wrote:
>
> > def remove_nl_and_kw file_name, key_word="ruby"
> > File.readlines(file_name).
> > map{|line| line.chomp}.
> > gsub(key_word, "_" << key_word << "_").
> > join
> >
> > end
> >
> > If you had the newlines removed because keywords might spwan lines you
> > have to put join in front of gsub, but that might become a performance
> > nightmare for larger files as you will be calling gsub on *huge*
> > strings.
>
> Robert,
>
> Nice tight illustrative code, but it needs a newbie correction. If join
> is to be at the end, then you need
Well spotted, thx for correcting it :), will teach me to copy lines around.
>
> def remove_nl_and_kw( file_name, key_word="ruby")
> File.readlines(file_name).
> map{|line| line.chomp.gsub(key_word, "_" << key_word << "_")}.
that's it very nice
> join
> end
>
> Now I have another newbie question. When is the file closed?
> end of readlines?
that's it, as a matter of fact this is one of my favorite properties
of the class methods
#IO.read, #IO.readlines and #IO.open with a block.
of program?
>
> Thanks for patience.
> Ian
Thank *you*
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck