試圖用ruby編程計算器,我想把所有東西都放到一個數組中,這樣我就可以用gsub輸出單詞爲數學符號。拆分空間和非數字...包括非數字分隔符,不包括空格分隔符
answer ||= 0
puts "CALCULATE"
puts "#{answer}"
input = gets.chomp.split(/([\s\D])/)
# for test
puts "response #{input}"
puts "asdf" unless input.is_a? Array
puts "qwer array inputs: #{input.length}"
研究它永遠,很多很多的感謝
尋找
8*15 => ["8", "*", "15"]
eight times 15 => ["eight", "times", "15"]
8 divided by 15 => ["8", "divided", "by", "15"]
也許'split(/ [\ s \ b] /)' – Stefan