Forum: Ruby match([0].to_a[0] meaning?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Zhenning G. (Guest)
on 2008-12-13 12:28
my_ip = (require 'open-uri' ; open("") { |f|
/([0-9]{1,3}\.){3}[0-9]{1,3}/.match([0].to_a[0] })

what is the different between match([0].to_a[0] and match(

is someone can  explain it?
Sebastian H. (Guest)
on 2008-12-13 13:02
(Received via mailing list)
Zhenning G. wrote:
> what is the different between match([0].to_a[0] and match(

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

Since however, in this case, the matched string can't contain more than
line the whole to_a[0] business is superfluous.

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

This topic is locked and can not be replied to.