我有这个:
sentence.each_char {|char|
......
......
}
我想要这个:
sentence.each_char {|char|
if (char is the last char)
......
end
}
有人知道我该怎么做吗?
答案 0 :(得分:21)
length = sentence.length
sentence.each_char.with_index(1){|char, i|
if i == length
...
end
}
答案 1 :(得分:12)
您正在寻找'with_index'选项
sentence.each_char.with_index {|char, index|
if (index == sentence.length-1)
......
end
}