0

红宝石:2.3.7

导轨:4.2.10

我有以下实现,我想使用 Refinements 将方法添加到 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'

我在这里做错了什么?

4

0 回答 0