# Patterns of semantic contradictions?

**URL:** <https://www.ruby-forum.com/t/patterns-of-semantic-contradictions/84824>\
**Category:** Ruby\
**Created:** [February 8, 2007, 12:02am UTC](https://www.ruby-forum.com/t/patterns-of-semantic-contradictions/84824 "2007-02-08T00:02:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [February 8, 2007, 12:02am UTC](https://www.ruby-forum.com/t/patterns-of-semantic-contradictions/84824/1 "2007-02-08T00:02:58Z")

</div>

Hi list

I just could not resist to post a stupid remark - which was taken very  
gracefully ty 😉 - to a thread where somebody corrected his typo with  
the  
well known sub idiom, only that he did something like

“abcdefghijklmnop”.gsub(/a/,“A”)

I am kind of troubled with this, of course this is equivalent to using  
#sub,  
and the question that arose in my head is the following.  
Is this a pattern to avoid? Can there be good reasons to do this and  
furthermore and most importantly are there some publications about  
“Antipatterns” like that. Maybe on much grater scale than my fun  
example.

Well it is time to explain why it is an Antipattern to me, maybe you do  
not  
agree at all.

being a code reviewer and seeing 5 lines later  
“abcda”.gsub(/a/,“A”)  
I will be troubled, or should I not? Is it sound to assume that the  
coder  
knows what she is doing?

Long live extreme programming 😉

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [February 8, 2007, 12:17am UTC](https://www.ruby-forum.com/t/patterns-of-semantic-contradictions/84824/2 "2007-02-08T00:17:01Z")

</div>

On Thu, 8 Feb 2007, Robert D. wrote:

> Is this a pattern to avoid? Can there be good reasons to do this and  
> furthermore and most importantly are there some publications about  
> “Antipatterns” like that. Maybe on much grater scale than my fun example.
> 
> Well it is time to explain why it is an Antipattern to me, maybe you do not  
> agree at all.
> 
> being a code reviewer and seeing 5 lines later  
> “abcda”.gsub(/a/,“A”)  
> I will be troubled, or should I not? Is it sound to assume that the coder  
> knows what she is doing?

hmmm. hard to say without context, but i generally squirm at any regex  
that  
has neither anchors nor uses the positive, rather than negative - eg i  
prefer

pathname =~ %r| IMG [^/]+ .jpg $ |iox

to

pathname =~ %r/ IMG.\*jpg /iox

because it’s can’t sweep down path separators.

interesing thread…

-a

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [February 8, 2007, 8:17am UTC](https://www.ruby-forum.com/t/patterns-of-semantic-contradictions/84824/3 "2007-02-08T08:17:29Z")

</div>

On 2/8/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Thu, 8 Feb 2007, Robert D. wrote:
> 
> hmmm. hard to say without context, but i generally squirm at any regex  
> that  
> has neither anchors nor uses the positive, rather than negative - eg i  
> prefer
> 
> pathname =~ %r| IMG [^/]+ .jpg $ |iox

that is very good advice in practice and it is also an excellent example  
for  
my dilemma.  
Redundancy (just imagine for the sake of argument that your regexp is  
redundant in the space of strings it is applied to, which normally it is  
not  
of course) is a very good thing sometimes.  
Your regexp tells the reader that you definitely do not want to match  
“IMGS001/draft.jp” which might be pretty good for readability and  
extensability, even if there are no such strings.

“a”.gsub(/a/,“A”) might have the same autocommenting effect in some  
contexts, but it might be confusing too, I guess the team needs to talk  
about this kind of ambiguity (think larger scale than the fun example  
please).

What I was (or rather develop to be) up to is if there is a  
categorization  
of such things (on much a larger scale, sry if I repeat myself).  
When we implement patterns, sometimes we refactor. When we do this do we  
not  
in some way look for Antipatterns to transform them into Patterns?

I would love to know more about that approach if it exists which I do  
not  
really doubt, gotta ask Google too 😉

to

> pathname =~ %r/ IMG.\*jpg /iox
> 
> because it’s can’t sweep down path separators.
> 
> interesing thread…

thx 🙂

-a

> –  
> we can deny everything, except that we have the possibility of being  
> better.  
> simply reflect on that.
> 
> - the dalai lama
> 
> Cheers  
> Robert

---

<div class="post-metadata">

**Author:** ![flatekmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e8c25b/32.png) [@flatekmonkey](https://www.ruby-forum.com/u/flatekmonkey)\
**Post date:** [February 8, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/patterns-of-semantic-contradictions/84824/4 "2007-02-08T18:36:35Z")

</div>

Robert D. wrote:

> “abcdefghijklmnop”.gsub(/a/,“A”)

gsub seems out of place here; it feels like using a flat-headed  
screwdriver to turn a star-headed screw. Instead, I would use tr:

“abcdefghijklmnop”.tr ‘a’, ‘A’

> “abcda”.gsub(/a/,“A”)

Same here.
