From: Hal Fulton Date: 2003-09-14T17:50:50+09:00 Subject: Re: Does String#scan(/(..)(..)/) produce an array of arrays? RLMuller wrote: > Hi All, > > "Programming Ruby" says the following for String#scan at > http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.scan: > > > |a = "cruel world" | > > |...| > > |a.scan(/(..)(..)/) |� |[["cr", "ue"], ["l ", "wo"]]| > > || > > |I wrote the following to test whether scan actually produced an array > or arrays:| > > || > > |a = "cruel world" > ar = a.scan(/(..)(..)/) > puts "Type of ar = %s of size %d" % [ar.class, ar.size] > ix=0 > ar.each {|x| puts "Type of ar[#{ix}] is %s" % [ix, ar[ix].class]; ix+=1} > | > > |That resulted in:| > > || > > |Type of ar = Array of size 2 > Type of ar[0] is 0 > Type of ar[1] is 1| > > || > > |So it seem like we don't have an array of arrays. So what do we have? > Or am I all wet?| You're all wet. ;) Seriously, you've just made a typo or two. Your last format string does an interpolation instead of using a format specifier. You're printing the value of ix as the class. I suggest each_with_index and consistent formatting, like ar.each_with_index {|x,ix| puts "Type of ar[#{ix}] is #{ar[ix].class}" } If you don't like variable interpolation, you can use a real printf: printf "Type of ar[%d] is %s\n",ix,ar[ix].class Anyhow, you do get real live arrays here, just as the Book says. Hal