ruby - File.exist? always returns false even when file does exist -


i have program tries open file:

dir.chdir(file.dirname(__file__)) puts "enter file name: "; relpath = gets; abspath = dir.pwd << "/" << relpath; if file.exist?(abspath)     puts "file exists";     file = file.open(abspath, "r");     other code... else     puts "file not exist"; end 

it prints "file not exist" when current directory exists , file exists. file , script in same directory.

i running on mac os x yosemite (10.10.3) , ruby 2.2.0p0.

i can't explain why (albeit have strong belief it's whitespace characters) little contribution works ok.

dir.chdir(file.dirname(__file__)) print "enter file name:"; relpath = gets.chomp; #intuitively used this, , wroked fine abspath = file.expand_path(relpath) #used builtin function expand_path instead of string concatenation puts abspath puts file.file?(abspath) if file.exist?(abspath)      puts "file exists";     puts file.ctime(abspath) #attempting dummy operation :)  else     puts "file not exist"; end 

runnning code

$ ls -a anal* analyzer.rb $ ruby -v ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux] ziya@ziya:~/desktop/code/ruby$ ruby fileexists.rb  enter file name:analyzer.rb /home/ziya/desktop/code/ruby/analyzer.rb #as result of puts abspath true #file.file?(abspath) => true file exists 2015-06-11 12:48:31 +0500 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -