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

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

Now I have another newbie question.  When is the file closed?
end of readlines?
end of statement?
end of function call?
end of program?

Thanks for patience.
Ian

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