If I change the snippet as recommended (twice over!) I get:

-:9: undefined local variable or method `comment?' for #<Object:0x40061860> (NameError)
        from -:8:in `each'
        from -:8
        from -:7:in `open'
        from -:7

>>> Dave Thomas <Dave / thomases.com> 03/15/00 01:36PM >>>
"David Douthitt" <DDouthitt / cuna.com> writes:

> Why does this snippet fail?
> 
>       class String
>          def String.comment (str)
>             str =~ /^(#| *$)/
>          end
>       end

This defines a class method (also called by Ruby a singleton
method). These methods are invoked without an object instance.

>       File.open(ENV["ORACONF"]) { |conf|
>          conf.readlines.each { |line|
>             next if line.comment

But here you're calling the method 'comment' on a String object. As
there is no instance method called 'comment', you'll get the failure.

> However, if the last line is converted to:
> 
>             next if String.comment(line)

This works because you're calling the class method.

If you wanted to do this using an instance method, you'd code
something like

       class String
          def comment?
             self =~ /^(#| *$)/
          end
       end

       ...

       next if line.comment?


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


Regards


Dave