Question

i use about 3 - 4 common Reqular Expressions inside most of models. so i want to put these regular expressions in module to use them directly instead of repeating it in every model. how can i accomplish that?

Était-ce utile?

La solution

module CommonRegularExpressions
  REGULAR_EXPRESSION_1 = /one/
  REGULAR_EXPRESSION_2 = /two/
end

class OverHere
  def call_me(input)
    return input[CommonRegularExpressions.REGULAR_EXPRESSION_1]
  end
end

class WithInclude
  include CommonRegularExpressions

  def call_me_too(input)
    return input.scan(REGULAR_EXPRESSION_2)
  end
end
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top