我有一個名爲quotes.txt的八行文件。當我運行下面的代碼時,它輸出一個包含8個項目並且長度爲1的數組。紅寶石長度不顯示數組的正確長度
我期待8而不是1.我在這裏做錯了什麼?
#!/usr/bin/env ruby
# myrandom_sig.rb
filename = ARGV[0] || (ENV['HOME'] + '/Documents/rubybyexample/RBE_scripts/quotes.txt')
quotation_file = File.new(filename, 'r')
file_lines = quotation_file.readlines()
quotation_file.close()
quotations = file_lines.to_s.split("\n\n")
puts quotations
puts quotations.length
輸出
➜ RBE_scripts ruby -w myrandom_sig.rb quotes.txt
["It is better to
have loved and lost than just to have lost.\n", "It is bad luck to be
superstitious.\n", "If it jams, force it. If it breaks, it needed
replacement anyway.\n", "Always remember that you are unique. Just
like everyone else.\n", "A woman without a man is like a fish without
a bicycle.\n", "A bachelor is a guy who is footloose and fiancee
free.\n", "If Yoda a great Jedi master he is, why not a good sentence
construct can he?\n", "People will remember you better if you always
wear the same outfit.\n"]
1
,請複製粘貼輸出'的提出quotations' – Josh 2014-09-04 00:46:56