question of regexp select

T

Tom Reilly

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 Reilly
 
D

daz

Tom said:
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)


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
 
M

Mark Hubbart

T24gMTEvMTMvMDUsIGRheiA8ZG9vYnlAZDEwLmthcm9vLmNvLnVrPiB3cm90ZToKPgo+IFRvbSBS
ZWlsbHkgd3JvdGU6Cj4gPiBSZWdleHAgZXJyb3I/Cj4gPiBQYWdlIDUzOCBQcm9ncmFtbWluZyBS
dWJ5Cj4gPgo+ID4gbSA9IC8oLikoLikoXGQrKShcZCkvLnNlbGVjdCgiVFhIMTEzODogVGhlIE1v
dmllIikKPiA+IHAgbS50b19hCj4gPgo+ID4KPiA+IHJiX3RtcC5yYjoxOiBwcml2YXRlIG1ldGhv
ZCBgc2VsZWN0JyBjYWxsZWQgZm9yIC8oLikoLikoXGQrKShcZCkvOlJlZ2V4cAo+ID4gKE5vTWV0
aG9kRXJyb3IpCj4gPiAgPkV4aXQgY29kZTogMQo+ID4KPiA+Cj4gPiBXaHkgc2hvdWxkIHRoZXJl
IGJlIGEgTm9NZXRob2RFcnJvciBmb3Igc2VsZWN0Pwo+ID4KPgo+Cj4gVGhlcmUncyBubyBtZXRo
b2QgUmVnZXhwI3NlbGVjdCwgQUZBSUsuCj4KPiBQaWNrQXhlIEkgc2hvd3M6Cj4KPiAgIG0gPSAv
KC4pKC4pKFxkKykoXGQpLy5tYXRjaCgiVEhYMTEzODogVGhlIE1vdmllIikKPgo+Cj4KPiBJcyB0
aGF0IGEgc2Vjb25kIGVkaXRpb24gbWlzcHJpbnQ/Cgo1MzggaXMgdGhlIHNlY3Rpb24gb24gTWF0
Y2hEYXRhLCB3aGljaCBoYXMgYSAjc2VsZWN0LiBSZWdleHAgaGFzIG5vCiNzZWxlY3QgbWV0aG9k
LgoKY2hlZXJzLApNYXJrCg==
 
D

daz

Mark said:
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
 
D

daz

I said:
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 ;)

daz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top