Forum: Ruby question of regexp select

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.
F7b23ca97e51ae89341a19548aa63467?d=identicon&s=25 w3gat (Guest)
on 2005-11-14 00:14
(Received via mailing list)
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
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-11-14 00:38
(Received via mailing list)
Tom Reilly 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
D9179cdd918879d0510dfc56411e4772?d=identicon&s=25 discordantus (Guest)
on 2005-11-14 09:32
(Received via mailing list)
On 11/13/05, daz <dooby@d10.karoo.co.uk> 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
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-11-14 15:50
(Received via mailing list)
Mark Hubbart 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
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 decoux (Guest)
on 2005-11-14 16:11
(Received via mailing list)
>>>>> "d" == daz  <dooby@d10.karoo.co.uk> writes:

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

 sub(/select/, "values_at")


Guy Decoux
Dce0999389d102f9a313af625375304c?d=identicon&s=25 dooby (Guest)
on 2005-11-14 16:23
(Received via mailing list)
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 ;)

daz
This topic is locked and can not be replied to.