Ruby:在while循环之前和之后声明变量之间的区别?

时间:2016-10-15 07:47:41

标签: ruby object undefined

我正在研究某个问题时,我发现扔掉所有内容的是我在while循环之外声明了一个变量。有问题的变量是sub_string = 1.似乎我需要在第一个while循环之后声明它,就在它实际在第二个while循环中发挥作用之前。通过早期声明它,循环以某种方式只能拾取从字符串[0]开始的回文,然而,可以是任何长度。对于我的生活,我不确定它为什么会以这种方式表现,因为我宣称切片的长度(而不是起始位置)。

如果我错过了一些非常基本的东西,请原谅我,因为我对编码很新。

这是有问题的代码(省略了回文的定义?检查方法)。

def longest_palindrome(string)
  idx = 0
  longest = nil
  #substring_string = 1  ; declaring it early seems to cause weird issues to show up
  while idx < string.length
    substring_length = 1 #I have to declare the substring_length here

    while (idx + substring_length) <= string.length
      substring = string.slice(idx, substring_length)
      if palindrome?(substring) && (longest == nil || substring.length > longest.length)
    longest = substring
    print substring_length
  end

  substring_length += 1
end

  idx += 1
  end

  return longest
end

1 个答案:

答案 0 :(得分:0)

  

场景1:while循环中定义的 substring_length

def longest_palindrome(string)
  idx = 0
  longest = nil
  while idx < string.length
    substring_length = 1
    while (idx + substring_length) <= string.length
      # palindrome
      substring_length += 1 
    end
    idx += 1
  end
  longest
end
  • 在对象上调用此方法时,执行控制流将移至第一个while循环。 while idx < string.length
  • 在该循环中,您初始化 substring_length1
  • 然后代码进入嵌套循环while (idx + substring_length) <= string.length
  • 在嵌套循环中,调用回文方法,然后substring_length 递增1 。此代码将一直运行,直到其条件返回false (idx + substring_length) < string.length。这意味着,一旦这个内部循环结束,substring_length 总是将成为大于1的值
  • 一旦这个内部循环结束,控制就会回到外部while循环。现在代码的第一行重新分配 substring_length1。实质上,无论内部循环结束时substring_length的值是什么,一旦控制流回到外部循环,当您将substring_length设置为 1
  

场景2: substring_length在while循环中定义

def longest_palindrome(string)
  idx = 0
  longest = nil
  substring_length = 1
  while idx < string.length
    while (idx + substring_length) <= string.length
      # palindrome
      substring_length += 1 
    end
    idx += 1
  end
  longest
end
  • 在对象上调用此方法时,substring_length 初始化为1 ,然后执行控制流移至第一个while循环。 while idx < string.length
  • 如果条件为真,则控制流将移至第二个循环inner循环
  • 在内环中,调用回文方法,然后substring_length 增加1 。此代码将一直运行,直到其条件返回false (idx + substring_length) < string.length。就像在方案1 中一样,这意味着一旦这个内部循环结束,substring_length 始终将是大于1的值
  • 一旦这个内循环结束,控制就会回到外部while 环。这次没有初始化语句,因此内部循环结束时substring_length的值是控制返回外部循环时使用的相同值

让我们尝试使用一个例子

情景1

longest_palindrome('him')
idx = 0; substring_length = 1; string.length = 3
while (0 < 3) # the outer loop condition is true,
substring_length = 1

# while (idx + substring_length) < string.length
while (0 + 1) < 3 # inner loop condition is true
# run code block, increment substring_length by 1, substring_length is NOW 2

while (0 + 2) < 3 # inner loop condition is still true
# run code block, increment substring_length by 1, substring_length is NOW 3

while (0 + 3) < 3 # inner loop condition fails, substring_length = 3

# Go to line after end of inner loop
increase idx by 1 # idx = 1

# Go back to outer loop
set substring_length to 1 # substring_length is NO MORE 3

# while (idx + substring_length) < string.length
while (1 + 1) < 3 # run code block, increment substring_length by 1, substring_length is NOW 2
while (1 + 2) < 3 # condition fails
..... and so on

场景2

longest_palindrome('him')
idx = 0; substring_length = 1; string.length = 3
while (0 < 3) # the outer loop condition is true,

# while (idx + substring_length) < string.length
while (0 + 1) < 3 # inner loop condition is true
# run code block, increment substring_length by 1, substring_length is NOW 2

while (0 + 2) < 3 # inner loop condition is still true
# run code block, increment substring_length by 1, substring_length is NOW 3

while (0 + 3) < 3 # inner loop condition fails, substring_length = 3

# Go to line after end of inner loop
increase idx by 1 # idx = 1

# Go back to outer loop. Remember substring_length =3

# while (idx + substring_length) < string.length
while (1 + 3) < 3 # condition is false. Inner loop is NOT EXECUTED

# Go to line after end of inner loop
increase idx by 1 # idx = 1

# Go back to outer loop while (idx < string.length)
while (1 < 3) # outer loop condition is true

# Go to inner loop while (idx + substring_length) < string.length
while (1 + 3) < 3 # Once again condition is false and inner loop is NOT EXECUTED

# Go to line after end of inner loop
increase idx by 1 # idx = 1

正如您在此场景中所看到的,内部循环停止执行,因为substring_length未被重新分配为1并且条件不断失败。