【问题标题】:How can I do this inside the each_char?如何在 each_char 中执行此操作?
【发布时间】:2013-06-05 04:36:14
【问题描述】:

我有这个:

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

我想要这个:

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

有人知道我该怎么做吗?

【问题讨论】:

标签: ruby


【解决方案1】:
length = sentence.length
sentence.each_char.with_index(1){|char, i|
  if i == length
    ...
  end
}

【讨论】:

    【解决方案2】:

    您正在寻找“with_index”选项

    sentence.each_char.with_index {|char, index|
      if (index == sentence.length-1)
       ......
      end
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-03
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多