4

https://ruby-doc.org/stdlib-2.6.3/libdoc/tmpdir/rdoc/Dir.html contains a description about the mktmpdir method of the Dir class.

mktmpdir(prefix_suffix=nil, *rest)
::mktmpdir creates a temporary directory.

An app uses it with no error but why it is not possible to run it in console / irb?

I have tested with Dir.new ... as well, below my attempt with Dir.mktmpdir.

irb(main):010:0> Dir.mktmpdir {|dir| dir is ".../d..." }
Traceback (most recent call last):
        5: from C:/Ruby26/bin/irb.cmd:31:in `<main>'
        4: from C:/Ruby26/bin/irb.cmd:31:in `load'
        3: from C:/Ruby26/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        2: from (irb):10
        1: from (irb):10:in `rescue in irb_binding'
NoMethodError (undefined method `mktmpdir' for Dir:Class)

Dir.methods is not showing this method but only mkdir.

What is missing respectively what is the reason for this behavior?

Christian
  • 4,902
  • 4
  • 24
  • 42

1 Answers1

10

You're confusing the patches to the Dir class that tmpdir in the standard library adds with the plain unpatched Dir class in core. If you look at the tmpdir package's documentation:

https://ruby-doc.com/stdlib/libdoc/tmpdir/rdoc/Dir.html

then you'll find Dir.mktmpdir (note the stdlib/libdoc/tmpdir) in the path. But if we look at Dir in core:

https://ruby-doc.org/core/Dir.html

you'll find all the usual class methods (chdir, chroot, ...) but no mktmpdir.

If you want to use mktmpdir then you have to require 'tmpdir':

Dir.method(:mktmpdir)
# NameError (undefined method `mktmpdir' for class `#<Class:Dir>')

require 'tmpdir'
Dir.method(:mktmpdir)
# #<Method: Dir.mktmpdir> 
mu is too short
  • 426,620
  • 70
  • 833
  • 800