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?

Was it helpful?

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top