--00163630e86748828b04a811e852
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Jul 14, 2011 at 8:12 PM, Chad Perrin <code / apotheon.net> wrote:

> On Fri, Jul 15, 2011 at 09:54:48AM +0900, Chad Perrin wrote:
> >
> > I'd probably do it something like this:
> >
> >     ARGF.each_line do |line|
> >       line.sub!(/#{reg1}.*/, '')
> >       puts line
> >     end
>
> Actually, I did too literal a translation of your code.  If I was writing
> this from scratch, I probably would have done this instead:
>
>    ARGF.each_line {|line| puts line.sub(/#{reg1}.*/, '') }
>
> . . . or this:
>
>    ARGF.each_line do |line|
>       puts line.sub(/#{reg1}.*/, '')
>    end
>
> Which I would choose would depend on whether I felt it looked better
> amidst my other code in the file on one line or three, and on whether I
> expected I might need to add more lines later.
>
> --
> Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]
>


Depending on how the string is to be interpreted, you may need to escape it
before creating it:

str  a.b"
/#{str}/ # /a.b/
Regexp.new str # /a.b/
Regexp.new Regexp.escape str # /a\.b/


Depending on how big the files are, efficiency may be very important. In
that case, you might benchmark a few different approaches and see which is
best. Here is another possibility:
ARGF.each_line do |line|
  index  ine.index regex
  line.slice!(index..-1) if index
  puts line
end

--00163630e86748828b04a811e852--