[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

match(f.read)[0].to_a[0] meaning?

Zhenning Guan

12/13/2008 10:21:00 AM

my_ip = (require 'open-uri' ; open("http://myi...) { |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?
--
Posted via http://www.ruby-....

1 Answer

Sebastian Hungerecker

12/13/2008 10:55:00 AM

0

Zhenning Guan 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
--
NP: Depeche Mode - Nothing
Jabber: sepp2k@jabber.org
ICQ: 205544826