Ruby NoMethod错误

时间:2017-01-28 11:47:15

标签: ruby-on-rails ruby

我正在尝试解决简单的任务:找到由两个3位数字的乘积制成的最大的回文:

Parent parent = this.getById(id);

    // Convert Date Type
    DateTime dayTimeEnd = new DateTime(date);
    dayTimeEnd = dayTimeEnd.plusHours(23);
    dayTimeEnd = dayTimeEnd.plusMinutes(55);
    Date dayEnd = dayTimeEnd.toDate();

    // Get all Tracking Data of this Date
    List<Child> ChildList = this.childRepository.findAllBytimeStampBetweenAndParent(date, dayEnd, parent);

但出了点问题:

def is_palindrome?(number)
  number = number.to_s
  while number.length > 1
    return false if number[0] != number[number.length - 1]
    number = number[1, number.length - 2]
  end
  return true
end

def find_max_palindrome
  x, y, z = 100
  max = 1
  while x < 1000
    while y < 1000
      z = x * y
      max = z if is_palindrome?(z)
      y += 1
    end
    x += 1
  end
  return max
end

puts find_max_palindrome

谁能告诉我哪里出错了?

2 个答案:

答案 0 :(得分:1)

观看这个

x, y, z = 100
 => 100 
2.4.0 :002 > x
 => 100 
2.4.0 :003 > y
 => nil 
2.4.0 :004 > z
 => nil 

你的意思是

x = y = z = 100

答案 1 :(得分:1)

x, y, z = 100

未将100分配给xyz。它只会将100分配给x。这会导致行while y < 1000引发错误,因为y仍为nil

将该行更改为:

x = y = z = 100

或以下我认为更容易理解和理解的内容:

x = 100
y = 100
z = 100