# Match(f.read)\[0\].to\_a\[0\] meaning?

**URL:** <https://www.ruby-forum.com/t/match-f-read-0-to-a-0-meaning/155554>\
**Category:** Ruby\
**Created:** [December 13, 2008, 11:28am UTC](https://www.ruby-forum.com/t/match-f-read-0-to-a-0-meaning/155554 "2008-12-13T11:28:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 13, 2008, 11:28am UTC](https://www.ruby-forum.com/t/match-f-read-0-to-a-0-meaning/155554/1 "2008-12-13T11:28:22Z")

</div>

my\_ip = (require ‘open-uri’ ; open(“[http://myip.dk](http://myip.dk)”) { |f|  
/([0-9]{1,3}.){3}[0-9]{1,3}/.match(f.read)[0].to\_a[0] })

what is the different between match(f.read)[0].to\_a[0] and match(f.read)  
?

is someone can explain it?

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 13, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/match-f-read-0-to-a-0-meaning/155554/2 "2008-12-13T12:02:36Z")

</div>

Zhenning G. wrote:

> what is the different between match(f.read)[0].to\_a[0] and match(f.read)

match(f.read) returns a MatchData object.  
match(f.read)[0] returns a String object, which is the substring  
that  
the regular expression matched.(\*)  
match(f.read)[0].to\_a returns an Array object where each item of the  
array  
is a line of the string.  
match(f.read)[0].to\_a[0] returns the first item of that array, i.e. the  
first  
line of the string.

Since however, in this case, the matched string can’t contain more than  
one  
line the whole to\_a[0] business is superfluous.

(\*) match\_data\_object[i], with 0 \< i \<= n, where n is the number of  
capturing  
groups in the regular expression, returns the substring matched by the  
ith  
capturing group of the regular expression.

HTH,  
Sebastian
