単純な数学文字列"1 2 3 * + 4 5 - /"
を のような整数および/または記号の配列に変換しようとしてい[1, 2, 3, :*, :+, 4, 5, :-, :/]
ます。
これよりもエレガントな (そして拡張可能な) ソリューションはありますか?
def tokenize s
arr = s.split(/ /)
symbols = %w{ + - / * }
arr.collect! do |c|
if symbols.include?(c)
c.to_sym
else
c.to_i
end
end
end