Ruby未定义的变量方法

时间:2016-05-31 21:07:29

标签: ruby nomethoderror

我有一些代码:

def strip_str(str)
  if str.nil?
    return ""
  end
  return I18n.transliterate(UnicodeUtils.downcase(str.force_encoding("utf-8"))).gsub(/\s+/, " ")
end

city_info = []

transactions.each do |trx|
  city = strip_str(trx["city"])
  event_city = strip_str(trx["event_city"])

  cities_dist = Levenshtein.distance city event_city
  cities_normal_dist = Levenshtein.normalized_distance city event_city
  cities_equal = (city == event_city)

  city_info << [cities_dist, cities_normal_dist, cities_equal]
end

这通常适用于transactions中的一堆元素,但最终只有city = 'manaus'event_city = 'manaus'的一个点,我得到:

NoMethodError: undefined method `city' for main:Object
cities_equal = (city == event_city)行上

。这对我来说似乎毫无意义,因为我只是比较两个字符串而没有一个被称为方法。这段代码有什么问题?

0 个答案:

没有答案