Question

I looked the Dir.exists? method up in the documentation and I believe I am using it correctly, yet at each call I am hit with this error:

undefined method `exists?' for Dir:Class (NoMethodError)

Am I overlooking something incredibly obvious? Below is the related code.

#!/usr/bin/ruby
ARGV.each do |dir|          # Arguements are directory names.
    if !Dir.exists?(dir)
#dosomething
end
end

EDIT:

Using File.directory? instead of Dir.exists? fixes my program, yet does not explain this error.

Was it helpful?

Solution

Which version of Ruby are you using? The documentation link you provided is for Ruby 2.0. The Dir.exists? method was not available in 1.8.

You can check your Ruby version from the command line with:

ruby -v

Or from an IRB session, check the value of the RUBY_VERSION constant.

If you're interested in using newer/multiple Ruby versions on your machine, I recommend looking into RVM or rbenv.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top