2017-10-20 38 views
-1

我不知道它可能的,但這裏是我想要實現:紅寶石使用讀取CSV文件中獲取

Please enter file path to analyse your expenses : 
user enters statment.csv 
and programs reads data. 

我的代碼:

require 'csv' 

puts "Please enter file path to analize" 
    file = gets 
data = CSV.read(file, headers: true) 
> 
> 
> 

該文件couldn這麼遠的消息」不被發現。 所有文件都在同一個文件夾中。 桌面/ expenses_calculator

Ë

+1

'pp'是你的朋友調試Ruby程序時。嘗試使用'require'pp',然後使用'pp file'來查看'file'包含的內容。你會自己看到問題。 – Casper

回答

1

什麼的file你輸入的東西后的價值?

puts 'Please enter file path to analize' 
file = gets 
p file 
# => "some_file.csv\n" 

最有可能要添加String#chomp其中「返回從字符串的結尾(如果存在)刪除指定的記錄分隔一個新的String。」

您可以用類似嘗試:

puts 'Please enter file path to analize' 
file = gets.chomp 
data = CSV.read(file, headers: true)