Quite simply, Ruby is supposed to be about consistency … Having the
“everything is an object, principal of least surprise” mantra, then
using these which act like a global ( $ ) but aren’t actually ( local
scope ) is just vile.
You have made a common mistake, you are thinking Ruby is meant for your
principle of least surprise, when it is matz’s least surprise…
Quoting Matz,
"Besides that, he doesn’t understand what POLS means. Since someone
will surprise for any arbitrary choice, it is impossible to satisfy
“least surprise” in his sense. The truth is two folds: a) when there
are two or more choices in the language design decision, I take the
one that makes me surprise least. "
I’m kind of new here, but it seems like debating the quality of one
another’s rudeness is slightly OT.
Incidentally, I earned my living as a back-end Perl programmer for a
couple years early in my career. I wrote literate OO code in Perl. The
thing I loved about Perl was that, like a natural language, there were
several ways to say the same thing some obvious, some flowery, some a
bit too terse. Among the things I did not like about Perl were some of
the whacky implicit variables like $1, etc, and especially @_. However,
I never needed to use them explicitly, because when I wanted them they
were always there implicitly (Those who know Perl know what I am
saying.)
My point is this: I am (or was) a Perl programmer and I do not like the
“Perlish” syntax one bit. I didn’t like it in Perl, and I don’t like it
in Ruby either. However, I object to the notion that Perlishness makes
it offensive. Non-obviousness is what makes it offensive. The matcher
syntax is much clearer.
I never called out anyone in particular, didn’t have anyone in mind in
fact, but it’s funny how some people are insecure enough that they’re
getting offended by what I wrote, and getting so defensive that they
feel the need to lash out in return.
I’m not sure if anyone lashed out really, just pointed out your
rudeness for your own good. You see, by writing the way you have here,
you have sewn bad will, and any further communication from you will
automatically be tainted by your previous behavior. Your opinion will
automatically be deemed less important than most other people. I’m
sure you don’t care, in your infinite wisdom and unfailing purity of
perfect thought, but I figured you should know.
It reminds me of a saying: “If you throw a rock into a pack of dogs, the
dog that yelps loudest is the one that got hit.”
I’m kind of new here, but it seems like debating the quality of one
another’s rudeness is slightly OT.
You are right of course, plus I was starting to get hypocritical and
be rude myself.
My point is this: I am (or was) a Perl programmer and I do not like the
“Perlish” syntax one bit. I didn’t like it in Perl, and I don’t like it
in Ruby either. However, I object to the notion that Perlishness makes
it offensive. Non-obviousness is what makes it offensive. The matcher
syntax is much clearer.
I had a strong dislike for Perl after some bad experiences at one job,
but for some reason the “Perlisms” in Ruby don’t bother me as much,
because as a whole Ruby tends to be so much more readable. Like
anything, I think there are times when using the $1 variables is
appropriate, and times when using MatchData is appropriate. To say one
or the other is the only proper way to code Ruby regular expression
matching is getting a little too pedantic.
In the same way I can’t control perceived mailing list “rudeness”, I
don’t think the people in the MatchData camp can control how other
people code (no matter how passionate they are about the topic.)
Especially on a section of Ruby code style that is not really debated
(whereas lots of people will denounce camelCaseMethods etc.)
Anyhow, even this discussion is off-topic for the original post, so
I’m stopping here.
well, i did not realise that the term “group’s captures” is that rare.
Match m = r.Match(text);
}
capture: [red]
it just must be as powerful as .net ; -)
konstantin
I don’t know whether your question was answered in the lengthy thread
already. In case not: in Ruby to get all matches of a group you need to
iterate through the whole string with #scan. There is no such thing as
this
feature of .net - and frankly I haven’t missed it so far. To get at all
the
words in your example this is sufficient:
s = “One car red car blue car”
=> “One car red car blue car”
s.scan /\w+/
=> [“One”, “car”, “red”, “car”, “blue”, “car”]
If you actually need group matches, you’ll have to do something like
this
ma=[]
=> []
s.scan(/\w(\w+)/) {|m| ma << m[0]}
=> “One car red car blue car”
ma
=> [“ne”, “ar”, “ed”, “ar”, “lue”, “ar”]
Of course this is quite a silly example… The main point here is that
you
must refrain from anchoring the regexp at the beginning if you want to
iterate like this.