为什么我的 foo()
函数打印出长度为 5 的字符串“YAAR”?
def foo()
map = Hash.new
File.open('dictionary.txt').each_line{ |s|
word = s.split(',')
if word.any? { |b| b.include?('AA') }
puts word.last
puts word.last.length
end
}
end
一些文件.txt
265651,YAAR
265654,YAARS
输出
YAAR
5
YAARS
6
最佳答案
你在两个字符串的末尾都有一个换行符 '\n'。因此,您的拆分收到:
"265651,YAAR\n"
"265654","YAARS\n"
关于ruby - 对 Ruby 长度属性感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125602/