# Question of regexp select

**URL:** <https://www.ruby-forum.com/t/question-of-regexp-select/47796>\
**Category:** Ruby\
**Created:** [November 14, 2005, 12:14am UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796 "2005-11-14T00:14:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![w3gat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@w3gat](https://www.ruby-forum.com/u/w3gat)\
**Post date:** [November 14, 2005, 12:14am UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796/1 "2005-11-14T00:14:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![w3gat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@w3gat](https://www.ruby-forum.com/u/w3gat)\
**Post date:** [November 14, 2005, 12:38am UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796/2 "2005-11-14T00:38:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![w3gat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@w3gat](https://www.ruby-forum.com/u/w3gat)\
**Post date:** [November 14, 2005, 9:32am UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796/3 "2005-11-14T09:32:51Z")

</div>

On 11/13/05, daz [removed\_email\_address@domain.invalid](mailto: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

---

<div class="post-metadata">

**Author:** ![w3gat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@w3gat](https://www.ruby-forum.com/u/w3gat)\
**Post date:** [November 14, 2005, 3:50pm UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796/4 "2005-11-14T15:50:04Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![w3gat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@w3gat](https://www.ruby-forum.com/u/w3gat)\
**Post date:** [November 14, 2005, 4:11pm UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796/5 "2005-11-14T16:11:07Z")

</div>

> > > > > “d” == daz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

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

sub(/select/, “values\_at”)

Guy Decoux

---

<div class="post-metadata">

**Author:** ![w3gat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@w3gat](https://www.ruby-forum.com/u/w3gat)\
**Post date:** [November 14, 2005, 4:23pm UTC](https://www.ruby-forum.com/t/question-of-regexp-select/47796/6 "2005-11-14T16:23:08Z")

</div>

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
