문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top