abhinavmsra
abhinavmsra

Reputation: 666

NoMethodError when using Refinements in an ActiveRecord Model

Ruby: 2.3.7

Rails: 4.2.10

I have the following implementation where I want to use Refinements to add a method to String class

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

As evident numeric? is the refined method on String class.

However, I fails with NoMethodError

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

What am I doing wrong here?

Upvotes: 1

Views: 222

Answers (0)

Related Questions