tags:

views:

251

answers:

2

Hi Ive recently been trying out the pathname lib in ruby and one thing I want to do is use mkdir to create some directories. Ive looked up the documentation and it says it wraps up mkdir but everytime I try to use it I`m getting this error

irb(main):006:0> p = Pathname.new('/tmp')
=> #<Pathname:/tmp>
irb(main):007:0> a = p.mkdir("123adam")
TypeError: can't convert String into Integer
    from /usr/lib/ruby/1.8/pathname.rb:975:in `mkdir'
    from /usr/lib/ruby/1.8/pathname.rb:975:in `mkdir'
    from (irb):7
    from /usr/lib/ruby/1.8/fileutils.rb:1589

Can anyone explain what I`m doing wrong here.

+6  A: 

You need to specify the directory you want to create with Pathname. And then call mkdir. So this should work:

p = Pathname.new('/tmp/123adam')
p.mkdir

The argument you can supply are the permissions for the new directory.

Veger
ahhhh now i understand! Thank you so much!
adam
+2  A: 

Out of interest, the reason why you get can't convert String into Integer is because Pathname.mkdir is actually a wrapper around Dir.mkdir as follows:

def mkdir(*args) Dir.mkdir(@path, *args) end

The path represented by the Pathname object is passed as the first parameter to Dir.mkdir, followed by any parameters passed to Pathname.mkdir. The 2nd parameter for Dir.mkdir is the numeric access permissions that you would like the directory created to have. Hence in your example "123adam" is being passed to Dir.mkdir where a number is expected.

mikej