Ruby 隐藏证件号码的某几位(给证件号打码)

module RangeMasker
  def mask(start, len, char = "X")
    masked = dup
    masked[start, len] = char * [len, masked.length - start].min
    masked
  end
end

number = "12345678901234567890"
number.extend RangeMasker
number.mask(10, 2) => "1234567890XX34567890"
This entry was posted in ruby on rails. Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>