# A use case for strmask gem?

**URL:** <https://www.ruby-forum.com/t/a-use-case-for-strmask-gem/197381>\
**Category:** Ruby\
**Created:** [October 13, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/a-use-case-for-strmask-gem/197381 "2010-10-13T15:57:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 13, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/a-use-case-for-strmask-gem/197381/1 "2010-10-13T15:57:50Z")

</div>

String::Mask is a library I created not for a specific use case, but  
simply b/c it had a certain logical sense to it. However, to this day  
I have not conceived of any need for it. I’m wondering if anyone else  
can think of a case where it might be useful.

I created a QED file which explains it pretty well:

[http://github.com/rubyworks/strmask/blob/master/qed/strmask.rdoc](http://github.com/rubyworks/strmask/blob/master/qed/strmask.rdoc)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [October 13, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/a-use-case-for-strmask-gem/197381/2 "2010-10-13T16:32:59Z")

</div>

On Wed, Oct 13, 2010 at 3:54 PM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> String::Mask is a library I created not for a specific use case, but  
> simply b/c it had a certain logical sense to it. However, to this day  
> I have not conceived of any need for it. I’m wondering if anyone else  
> can think of a case where it might be useful.
> 
> I created a QED file which explains it pretty well:

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

Cheers

robert

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [October 14, 2010, 9:22am UTC](https://www.ruby-forum.com/t/a-use-case-for-strmask-gem/197381/3 "2010-10-14T09:22:47Z")

</div>

On Wed, Oct 13, 2010 at 8:53 PM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> can think of is blacking-out a credit card number.
> 
> “4111-1234-1234-1234”.mask(‘_') + "_\_\__-XXXX-XXXX-XXXX".mask('_’)
> 
> #=\> “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 = StringMask.new(“\_\_\_\_-XXXX-XXXX-XXXX”)  
cc\_mask.apply(“4111-1234-1234-1234”) # =\> “4111-XXXX-XXXX-XXXX”  
cc\_mask[“4111-1234-1234-1234”] # =\> “4111-XXXX-XXXX-XXXX”

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

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [October 13, 2010, 9:00pm UTC](https://www.ruby-forum.com/t/a-use-case-for-strmask-gem/197381/4 "2010-10-13T21:00:48Z")

</div>

On Oct 13, 10:32am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the mask do? Apparently there are operations available on masks but  
> how do I apply a mask - and what to? And what’s the result of  
> applying a mask to something? What 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('_’)

#=\> “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?
