How to step through a loop with pry and view the value of an iterator?

Deadly 提交于 2019-12-09 04:42:26

You can type p i and p j to manually inspect them.

That'd make me crazy though, so I'd insert puts i and puts j temporarily.

Don't use for loops with Ruby. Instead we use each or upto, downto or times, depending on our purpose. Also, explicit return statements aren't needed at the end of a method unless you are forcing the code to exit early.

I'd write your code something like:

require 'pry'

def longest_palindrome(s)
  max_palindrome_len = 0

  s.length.times do |i|
    binding.pry
    i.upto(s.length) do |j|
      binding.pry
      substr = s[i..j]
      if substr == substr.reverse && substr.length > max_palindrome_len
        max_palindrome_len = substr.length
      end
    end
  end

  max_palindrome_len
end

longest_palindrome "racer"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!