[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01862] Re: Another question from a newbie

From: Dave Thomas <Dave@...>
Date: 2000-03-15 20:37:31 UTC
List: ruby-talk #1862
"David Douthitt" <DDouthitt@cuna.com> writes:

> Thanks for the help!  Now a question on your alternative:
> 
> >>> Dave Thomas <Dave@thomases.com> 03/15/00 01:36PM >>>
> 
> By the way, there's another fun Ruby trick for doing this kind of
> thing. You could code up a simple iterator which only returns
> non-blank non-comment lines
> 
>    def nonCommentLines(aFile)
>       aFile.each { |line|
>         line.gsub!(/#.*/, '')
>         yield(line) unless line =~ /^\s*$/
>       }
>    end
> 
>    File.open(ENV["ORACONF"]) { |conf|
>       nonCommentLines(conf) { |line|
>         print line
>       }
>    }
> 
> >>>snip!<<<
> 
> Isn't this alternative more costly in terms of time?  I know OOP isn't
> known for being fast, but....  It also seems to be much more complex -
> a yield and gsub! instead of a simple pattern-match.

Yup! I think it is. The gsub is there because it has slightly
different semantics than your version, skipping any line that consists 
solely of zero or more spaces after comments are removed. The lines it 
returns have the comments already stripped, ready for parsing, so

   db = fred     # primary database
                 # (a silly example)
   #
   size = 10     # and the size

will just return the lines

   db = fred
   size = 10

But, as you say, that does come at a cost.

However, the benefit of the approach is you now have a reusable method 
that will strip comments and blank lines from _any_ Enumerable
collection of strings (despite the name of the parameter). That may or 
may not be a big win the next time you have to parse a parameter file.

I could be argued either way (and frequently am)

Dave



In This Thread

Prev Next