# Regex loop yields single element array for multiple finds

**URL:** <https://www.ruby-forum.com/t/regex-loop-yields-single-element-array-for-multiple-finds/59110>\
**Category:** Ruby\
**Created:** [April 28, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/regex-loop-yields-single-element-array-for-multiple-finds/59110 "2006-04-28T19:28:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [April 28, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/regex-loop-yields-single-element-array-for-multiple-finds/59110/1 "2006-04-28T19:28:44Z")

</div>

I have a string with say “@@@asdfasdf@@@ @@@ lk;lk@@@” called markup,  
and I do:

markup.scan(/@@@/) do |ats|  
print ats.class  
print ats.length  
end

and I get the class as always Array, and the length as 1. What’s with  
that?

xc

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [April 28, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/regex-loop-yields-single-element-array-for-multiple-finds/59110/2 "2006-04-28T19:31:56Z")

</div>

On Sat, 29 Apr 2006, Xeno C. wrote:

> I have a string with say “@@@asdfasdf@@@ @@@ lk;lk@@@” called markup, and I do:
> 
> markup.scan(/@@@/) do |ats|  
> print ats.class  
> print ats.length  
> end
> 
> and I get the class as always Array, and the length as 1. What’s with that?
> 
> xc

## harp:~ \> ri String.scan ------------------------------------------------------------ String#scan str.scan(pattern) =\> array str.scan(pattern) {|match, …| block } =\> str

```
  Both forms iterate through _str_, matching the pattern (which may
  be a +Regexp+ or a +String+). For each match, a result is 

```

generated  
and either added to the result array or passed to the block. If  
the  
pattern contains no groups, each individual result consists of the  
matched string, +$&+. If the pattern contains groups, each  
individual result is itself an array containing one entry per  
group.

```
     a = "cruel world"
     a.scan(/\w+/) #=> ["cruel", "world"]
     a.scan(/.../) #=> ["cru", "el ", "wor"]
     a.scan(/(...)/) #=> [["cru"], ["el "], ["wor"]]
     a.scan(/(..)(..)/) #=> [["cr", "ue"], ["l ", "wo"]]

  And the block form:

     a.scan(/\w+/) {|w| print "<<#{w}>> " }
     print "\n"
     a.scan(/(.)(.)/) {|a,b| print b, a }
     print "\n"

  _produces:_

     <<cruel>> <<world>>
     rceu lowlr

```

-a

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [April 28, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/regex-loop-yields-single-element-array-for-multiple-finds/59110/3 "2006-04-28T19:47:11Z")

</div>

On Apr 28, 2006, at 7:26 PM, Xeno C. wrote:

> I have a string with say “@@@asdfasdf@@@ @@@ lk;lk@@@” called  
> markup, and I do:  
> […]  
> and I get the class as always Array, and the length as 1. What’s  
> with that?

“@@@asdfasdf@@@ @@@ lk;lk@@@”.scan(/@@@/) do |ats|  
ats # =\> “@@@”, “@@@”, “@@@”, “@@@”  
ats.class # =\> String, String, String, String  
end

You must be doing something else.

– Daniel

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [April 28, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/regex-loop-yields-single-element-array-for-multiple-finds/59110/4 "2006-04-28T19:50:19Z")

</div>

irb(main):090:0\* markup = “@@@asdfasdf@@@ @@@ lk;lk@@@”  
=\> “@@@asdfasdf@@@ @@@ lk;lk@@@”  
irb(main):091:0\> markup.scan(/@@@/) do |ats|  
irb(main):092:1\* puts ats.length  
irb(main):093:1\> end  
3  
3  
3  
3  
=\> “@@@asdfasdf@@@ @@@ lk;lk@@@”  
irb(main):094:0\>

On Apr 28, 2006, at 10:26 AM, Xeno C. wrote:

> xc

– Elliot T.

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog
