Forum: Ruby Whats wrong with this script?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
833348fb139ec4d62508eb9f0e28ac06?d=identicon&s=25 Tommy Nordgren (Guest)
on 2009-01-26 00:36
(Received via mailing list)
#!/usr/bin/ruby

$headersumfile = File.open("Header-summary.txt","w");

$finalizerfile = File.open("Finalizers.m","w");

def scanfiles(dirhand, dirname)

  dirhand.each { | fn |
    if (fn == '.' || fn == '..')

    elsif (fn =~ /\.m$/)
      File.open("#{dirname}/#{fn}","r") { | f |
        f.each { | l |
          $finalizerfile.puts(l);
        }
      }
    elsif (fn =~ /\.h$/)
      File.open("#{dirname}/#{fn}","r") { | f |
        f.each { | l |
          $headersumfilefile.puts(l);
        }
      }
    elsif (File.directory?("#{dirname}/#{fn}"))
      scanfiles(Dir.open("#{dirname}/#{fn}"),"#{dirname}/#{fn}")
    end
  }
  dirhand.close;
end

scanfiles(Dir.open("Source"),"Source");

$finalizerfile.close;
$headersumfile.close;

Message from terminal:
emac$ ./extractdata.rb
./extractdata.rb:21:in `scanfiles': private method `puts' called for
nil:NilClass (NoMethodError)
  from ./extractdata.rb:20:in `each'
  from ./extractdata.rb:20:in `scanfiles'
  from ./extractdata.rb:19:in `open'
  from ./extractdata.rb:19:in `scanfiles'
  from ./extractdata.rb:9:in `each'
  from ./extractdata.rb:9:in `scanfiles'
  from ./extractdata.rb:25:in `scanfiles'
  from ./extractdata.rb:9:in `each'
  from ./extractdata.rb:9:in `scanfiles'
  from ./extractdata.rb:31
Work Directory: ~/Desktop/Drawkit/DrawKit/Trunk :

The script are supposed to copy objective source and header files to a
pair of files.
The directory to be scanned exists in my current work directory.
Can someone please help
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-01-26 00:42
(Received via mailing list)
Tommy Nordgren wrote:
> $headersumfilefile.puts(l);

Typo: there is no variable $headersumfilefile, only $headersumfile.

HTH,
Sebastian
1d53b088a989e069b94597c282eebbbc?d=identicon&s=25 Simon Krahnke (Guest)
on 2009-01-26 08:07
(Received via mailing list)
* Sebastian Hungerecker <sepp2k@googlemail.com> (00:40) schrieb:

> Tommy Nordgren wrote:
>> $headersumfilefile.puts(l);
>
> Typo: there is no variable $headersumfilefile, only $headersumfile.

That seems to be another reason not to use global variables.

mfg,                        simon .... l
This topic is locked and can not be replied to.