Forum: Ruby Patterns of semantic contradictions?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert D. (Guest)
on 2007-02-08 01:02
(Received via mailing list)
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
unknown (Guest)
on 2007-02-08 01:17
(Received via mailing list)
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
Robert D. (Guest)
on 2007-02-08 09:17
(Received via mailing list)
On 2/8/07, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> wrote:
>
> On Thu, 8 Feb 2007, Robert D. wrote:
> <snip>
>
> 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
Suraj K. (Guest)
on 2007-02-08 19:36
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.
This topic is locked and can not be replied to.