osx - Ruby saying file doesnt exist -
i'm new ruby, , writing test program of features down. here program
#!/usr/bin/env ruby class filemanager def read_file(filename) return nil unless file.exist?(filename) file.read(filename) end end if __file__ == $0 fm = filemanager.new puts "what file open?" fname = gets puts fm.read_file fname end as can see, simple. if comment first line of read_file method, error
no such file or directory - /users/macuser/projects/aptana\ studio\ 3\ workspace/ruby\ test/text (errno::enoent) /users/macuser/projects/aptana studio 3 workspace/ruby test/ruby.rb:6:in `read_file' /users/macuser/projects/aptana studio 3 workspace/ruby test/ruby.rb:15:in `<main>' when run program , use file: /users/macuser/projects/aptana\ studio\ 3\ workspace/ruby\ test/text
however, if run cat /users/macuser/projects/aptana\ studio\ 3\ workspace/ruby\ test/text, outputs hello, world!, should.
i don't believe it's permissions issue because own folder, in case i've tried running program root. also, have made sure fname actual name of file, not nil. i've tried both escaped , unescaped versions of path, along text or full path. know fact file exists, why ruby giving me error?
with gets filename filename includes newline \n.
you have remove in filename:
gets filename p filename #"test.rb\n" p file.exist?(filename) #false p file.exist?(filename.chomp) #true (and don't need mask spaces)
Comments
Post a Comment