在ActiveRecord模型中使用优化时出现NoMethodError

时间:2019-01-24 03:16:37

标签: ruby-on-rails ruby refinements

Ruby:2.3.7

路轨:4.2.10

我有以下实现,我想使用优化功能向String类添加方法

module StringExtensions
  refine String do
    def numeric?
      str = String.new(self)

      (/\A\d+\z/ =~ str) == 0
    end
  end
end

class ShippingAddress < Address
  using StringExtensions

  # ...

  private

  def normalise_address_1
    return unless address_1.numeric?

    self.address_1 = "Calle #{address_1}"
  end
end

显然numeric?是String类的改进方法。

但是,我失败了,NoMethodError

NoMethodError: undefined method `numeric?' for "20":String
from (pry):1:in `normalise_address_1'

我在这里做什么错了?

0 个答案:

没有答案