14

私はこれを持っています:

sentence.each_char {|char|
   ......
   ......
}

これ欲しい:

sentence.each_char {|char|
   if (char is the last char)
     ......
   end
}

どうすればそれができるか知っている人はいますか?

4

2 に答える 2

26
length = sentence.length
sentence.each_char.with_index(1){|char, i|
  if i == length
    ...
  end
}
于 2013-06-05T04:39:20.010 に答える
16

「with_index」オプションを探しています

sentence.each_char.with_index {|char, index|
  if (index == sentence.length-1)
   ......
  end
}
于 2013-06-05T04:40:18.160 に答える