# Gsub not working. Ruby thinks I'm in an array?

**URL:** <https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197>\
**Category:** Ruby\
**Created:** [September 21, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197 "2007-09-21T16:39:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197/1 "2007-09-21T16:39:02Z")

</div>

I’ve chunked up a huge text file into arrays of discrete text groups. I  
want to go into each of those text groups and convert a bunch of stuff.  
I’ve proved that I’ve got my array OK, and, I’ve proved that I can do a  
“put” of any of the discrete text groups. But, in my first gsub! to  
convert stuff in these text groups, Ruby is complaining that I’m doing a  
gsub! in an array. I’m getting the following error message:

Exception: private method `gsub’ called for #Array:0x2e40910

Here’s the lines where this starts:  
registries = []  
registries = xmlfile.scan(/(.\*? )\</registration\>/im)  
#puts registries[100]

registries.sort!  
registries.each do |registry|  
registry.gsub!(/\</\*registrationList\>/, “”)

Can someone help me understand what’s going on here?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197/2 "2007-09-21T16:50:07Z")

</div>

Peter B. wrote:

> Here’s the lines where this starts:  
> registries = []  
> registries = xmlfile.scan(/(.\*? )\</registration\>/im)  
> #puts registries[100]
> 
> registries.sort!  
> registries.each do |registry|  
> registry.gsub!(/\</\*registrationList\>/, “”)
> 
> Can someone help me understand what’s going on here?

If you have capturing groups in your regexp, scan will return an array  
of  
arrays, so you are indeed calling gsub! on the subarrays. So either  
iterate  
over registries.flatten or call gsub! on registry[0] instead of  
registry.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197/3 "2007-09-21T17:10:38Z")

</div>

Sebastian H. wrote:

> Peter B. wrote:
> 
> > Here’s the lines where this starts:  
> > registries = []  
> > registries = xmlfile.scan(/(.\*? )\</registration\>/im)  
> > #puts registries[100]
> > 
> > registries.sort!  
> > registries.each do |registry|  
> > registry.gsub!(/\</\*registrationList\>/, “”)
> > 
> > Can someone help me understand what’s going on here?
> 
> If you have capturing groups in your regexp, scan will return an array  
> of  
> arrays, so you are indeed calling gsub! on the subarrays. So either  
> iterate  
> over registries.flatten or call gsub! on registry[0] instead of  
> registry.
> 
> HTH,  
> Sebastian

Thanks, Sebastian. OK, I did a .flatten, which I read more about and  
looks like a very cool method. Now, I don’t get an error message, but, I  
don’t get any substitutions either! You suggest doing a gsub! on  
registry[0], but, that’s just the first entry. What about the hundreds  
more I need to work on?

I need to keep the sub-groups discrete, because, I’m going to need to  
sort them, again, based on criteria at the bottom of each group.

-Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197/4 "2007-09-21T21:50:48Z")

</div>

> registries = []  
> registries = xmlfile.scan(/(.\*? )\</registration\>/im)  
> #puts registries[100]
> 
> registries.sort!  
> registries.each do |registry|  
> registry.gsub!(/\</\*registrationList\>/, “”)

regmatch = /(.\*? )\</registration\>/im

# method one

registries = []  
xmlfile.scan( regmatch ) do |registry|  
registries \<\< registry.gsub %r{\</?registrationList\>}, ‘’  
end  
registries.sort!

# method two

registries = xmlfile.scan( regmatch ).flatten.map do |reg|  
reg.gsub %r{\</?registrationList\>}, ‘’  
end.sort

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/gsub-not-working-ruby-thinks-im-in-an-array/115197/5 "2007-09-21T18:05:19Z")

</div>

Peter B. wrote:

> > Peter B. wrote:
> > 
> > > registries.sort!  
> > > registries.each do |registry|  
> > > registry.gsub!(/\</\*registrationList\>/, “”)
> 
> Thanks, Sebastian. OK, I did a .flatten, which I read more about and  
> looks like a very cool method. Now, I don’t get an error message, but, I  
> don’t get any substitutions either!

Yes, I didn’t notice this the first time because I was only focussing on  
the  
error you were getting, but the gsub! is basically useless there. You’re  
using it to modify registry which is thrown away at the end of the  
iteration.  
each doesn’t modify the array over which it iterates for that you want  
map:  
registries.map! do |registry|  
registry.gsub(/\</\*registrationList\>/, “”)  
end  
(This assumes that registries is already flattened, otherwise use  
registry[0]  
instead of registry. Thinking about it that might actually be better  
because  
it saves you the flatten step)

> You suggest doing a gsub! on  
> registry[0], but, that’s just the first entry.

registry only ever has one entry because the scan regex has only one  
capturing  
group.

HTH,  
Sebastian
