A use case for strmask gem?

I

Intransition

Frankly, from the description it's not clear to me what this is
supposed to do. =A0What is the string, what is the mask and what does
the mask do? =A0Apparently there are operations available on masks but
how do I apply a mask - and what to? =A0And what's the result of
applying a mask to something? =A0What does escaping do?

Fair point.

The library defines logical operations for string content. You can use
it to "mask" strings is various ways. I guess the simplest example I
can think of is blacking-out a credit card number.

"4111-1234-1234-1234".mask('_') + "____-XXXX-XXXX-XXXX".mask('_')

#=3D> "4111-XXXX-XXXX-XXXX"

The "escape character" is just what ever "empty slot" character you
want to use, in this case we are using '_'.

Does that make it a little clearer?
 
R

Robert Klemme

Fair point.

The library defines logical operations for string content. You can use
it to "mask" strings is various ways. I guess the simplest example I
can think of is blacking-out a credit card number.

=A0"4111-1234-1234-1234".mask('_') + "____-XXXX-XXXX-XXXX".mask('_')

=A0#=3D> "4111-XXXX-XXXX-XXXX"

The "escape character" is just what ever "empty slot" character you
want to use, in this case we are using '_'.

The term "escape character" for me has a different connotation: it is
a character that is used to strip meta characters off their metaness -
or the opposite (giving ordinary characters special meaning, such as
\n in double quoted strings). I would rather call the underscore in
your example "wildcard character" or so.
Does that make it a little clearer?

Yes, definitively. To me a different design would be more natural though

cc_mask =3D StringMask.new("____-XXXX-XXXX-XXXX")
cc_mask.apply("4111-1234-1234-1234") # =3D> "4111-XXXX-XXXX-XXXX"
cc_mask["4111-1234-1234-1234"] # =3D> "4111-XXXX-XXXX-XXXX"

That also seems more efficient: you only create one mask instance and
apply it to multiple Strings.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top