Question of regexp select


#1

Regexp error?
Page 538 Programming Ruby

m = /(.)(.)(\d+)(\d)/.select(“TXH1138: The Movie”)
p m.to_a

rb_tmp.rb:1: private method `select’ called for /(.)(.)(\d+)(\d)/:Regexp
(NoMethodError)

Exit code: 1

Why should there be a NoMethodError for select?

Thanks
Tom R.


#2

Tom R. wrote:

Why should there be a NoMethodError for select?

There’s no method Regexp#select, AFAIK.

PickAxe I shows:

m = /(.)(.)(\d+)(\d)/.match(“THX1138: The Movie”)
p m.to_a #-> [“HX1138”, “H”, “X”, “113”, “8”]

Is that a second edition misprint?

daz


#3

On 11/13/05, daz removed_email_address@domain.invalid wrote:

(NoMethodError)

m = /(.)(.)(\d+)(\d)/.match(“THX1138: The Movie”)

Is that a second edition misprint?

538 is the section on MatchData, which has a #select. Regexp has no
#select method.

cheers,
Mark


#4

Mark H. wrote:

(NoMethodError)

538 is the section on MatchData, which has a #select. Regexp has no
#select method.

So is the example the same as in the rdoc?:
http://www.ruby-doc.org/core/classes/MatchData.html#M000496

m = /(.)(.)(\d+)(\d)/.match(“THX1138: The Movie”)
m.to_a #=> [“HX1138”, “H”, “X”, “113”, “8”]
m.select(0, 2, -2) #=> [“HX1138”, “X”, “113”]

i.e. it creates a MatchData object before calling select?

Therefore the confusion is due to a bug in MatchData#select -

:in `select’: wrong number of arguments (3 for 0) (ArgumentError)

<re.c>

static VALUE
match_select(argc, argv, match)
int argc;
VALUE *argv;
VALUE match;
{
if (argc > 0) {
rb_raise(rb_eArgError, “wrong number of arguments (%d for 0)”,
argc);
}
else { […]

rb_cMatch  = rb_define_class("MatchData", rb_cObject);

[…]
rb_define_method(rb_cMatch, “select”, match_select, -1);

</re.c>

daz


#5

“d” == daz removed_email_address@domain.invalid writes:

d> m.select(0, 2, -2) #=> [“HX1138”, “X”, “113”]

sub(/select/, “values_at”)

Guy Decoux


#6

I wrote:

Is that a second edition misprint?

m = /(.)(.)(\d+)(\d)/.match(“THX1138: The Movie”)
m.to_a #=> [“HX1138”, “H”, “X”, “113”, “8”]
m.select(0, 2, -2) #=> [“HX1138”, “X”, “113”]

MatchData#select is deprecated (May 2003) - use #values_at:

m = /(.)(.)(\d+)(\d)/.match(“THX1138: The Movie”)
p m.to_a #=> [“HX1138”, “H”, “X”, “113”, “8”]
p m.values_at(0, 2, -2) #=> [“HX1138”, “X”, “113”]

PickAxe bug.

(No need to post because Guy just answered :wink:

daz