# Gsub choking on fixnum

**URL:** <https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462>\
**Category:** Ruby\
**Created:** [May 21, 2009, 1:08am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462 "2009-05-21T01:08:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 1:08am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/1 "2009-05-21T01:08:48Z")

</div>

I recently upgrade Ruby from 1.8.5 to 1.8.6. With 1.8.5, gsub handled a  
fixnum just fine but choked on it in 1.8.6. Casting the fixnum to a  
string solved my problem but I’m wondering if this is a bug or it was a  
bug and now it’s working as intended or …?

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 1:20am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/2 "2009-05-21T01:20:42Z")

</div>

Hi,

In message “Re: gsub choking on fixnum”  
on Thu, 21 May 2009 08:08:55 +0900, Cali W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I recently upgrade Ruby from 1.8.5 to 1.8.6. With 1.8.5, gsub handled a  
|fixnum just fine but choked on it in 1.8.6. Casting the fixnum to a  
|string solved my problem but I’m wondering if this is a bug or it was a  
|bug and now it’s working as intended or …?

Choking code please.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 1:37am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/3 "2009-05-21T01:37:56Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: gsub choking on fixnum”  
> on Thu, 21 May 2009 08:08:55 +0900, Cali W.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |I recently upgrade Ruby from 1.8.5 to 1.8.6. With 1.8.5, gsub handled a  
> |fixnum just fine but choked on it in 1.8.6. Casting the fixnum to a  
> |string solved my problem but I’m wondering if this is a bug or it was a  
> |bug and now it’s working as intended or …?
> 
> Choking code please.
> 
> ```
> matz.
> 
> ```

Here’s the code in question, I’m using REXML Document

doc = Document.new(entry.embed\_code)  
doc.root.each\_element(‘//embed | //object’){ |elem|  
elem.attributes[‘height’] = 140 #this is line 57, see error  
below  
elem.attributes[‘width’] = 170  
}

The above code generated this error  
NoMethodError (private method `gsub' called for 140:Fixnum): c:/ruby/lib/ruby/1.8/rexml/text.rb:292:in `normalize’  
c:/ruby/lib/ruby/1.8/rexml/element.rb:1085:in `[]=' /app/controllers/application.rb:57:in `resize\_videos’  
c:/ruby/lib/ruby/1.8/rexml/element.rb:891:in `each' c:/ruby/lib/ruby/1.8/rexml/xpath.rb:53:in `each’  
c:/ruby/lib/ruby/1.8/rexml/element.rb:891:in `each' c:/ruby/lib/ruby/1.8/rexml/element.rb:393:in `each\_element’  
/app/controllers/application.rb:56:in `resize\_videos’

To fix it, I changed it to

doc = Document.new(entry.embed\_code)  
doc.root.each\_element(‘//embed | //object’){ |elem|  
elem.attributes[‘height’] = 140.to\_s  
elem.attributes[‘width’] = 170.to\_s  
}

I’m hardly an expert on Ruby or REXML, so any insight is much  
appreciated.

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 1:54am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/4 "2009-05-21T01:54:01Z")

</div>

Hi,

In message “Re: gsub choking on fixnum”  
on Thu, 21 May 2009 08:37:58 +0900, Cali W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Here’s the code in question, I’m using REXML Document  
|  
| doc = Document.new(entry.embed\_code)  
| doc.root.each\_element(‘//embed | //object’){ |elem|  
| elem.attributes[‘height’] = 140 #this is line 57, see error  
|below  
| elem.attributes[‘width’] = 170  
| }

The following patch should work.

```
          matz.

```

diff --git a/ChangeLog b/ChangeLog  
index b5bd805…1edb627 100644  
— a/ChangeLog  
+++ b/ChangeLog  
@@ -1,3 +1,8 @@  
+Thu May 21 08:50:58 2009 Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
+

- 
  - lib/rexml/text.rb (REXML::Text.normalize): call to\_s for input.

- [ruby-talk:337069]
- 

Mon May 18 21:40:11 2009 Tanaka A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

- lib/pathname.rb (Pathname#sub): suppress a warning.  
diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb  
index 2bc0042…a4a30b6 100644  
— a/lib/rexml/text.rb  
+++ b/lib/rexml/text.rb  
@@ -286,7 +286,7 @@ module REXML  
EREFERENCE = /&(?!#{Entity::NAME};)/
# Escapes all possible entities
def Text::normalize( input, doctype=nil, entity\_filter=nil )

- 

```
 copy = input

```

- 

```
 copy = input.to_s
 # Doing it like this rather than in a loop improves the speed
 #copy = copy.gsub( EREFERENCE, '&amp;' )
 copy = copy.gsub( "&", "&amp;" )

```

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 6:30am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/5 "2009-05-21T06:30:25Z")

</div>

Yukihiro M. wrote:

> ```
> matz.
> 
> ```
> 
> diff --git a/ChangeLog b/ChangeLog  
> index b5bd805…1edb627 100644  
> — a/ChangeLog  
> +++ b/ChangeLog  
> @@ -1,3 +1,8 @@  
> +Thu May 21 08:50:58 2009 Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> +
> 
> - 
> - lib/rexml/text.rb (REXML::Text.normalize): call to\_s for input.
> 
> - [ruby-talk:337069]
> - 
> 
> Mon May 18 21:40:11 2009 Tanaka A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> - lib/pathname.rb (Pathname#sub): suppress a warning.  
> diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb  
> index 2bc0042…a4a30b6 100644  
> — a/lib/rexml/text.rb  
> +++ b/lib/rexml/text.rb  
> @@ -286,7 +286,7 @@ module REXML  
> EREFERENCE = /&(?!#{Entity::NAME};)/
> # Escapes all possible entities
> def Text::normalize( input, doctype=nil, entity\_filter=nil )
> 
> - 
> 
> ```
> copy = input
> 
> ```
> 
> - 
> 
> ```
> copy = input.to_s
> # Doing it like this rather than in a loop improves the speed
> #copy = copy.gsub( EREFERENCE, '&amp;' )
> copy = copy.gsub( "&", "&amp;" )
> 
> ```

This is a change in the REXML code, if I have to make a change, I’m more  
comfortable with changing my own code. Is this change a REXML bug fix or  
simply a fix for my problem? I’m still trying to figure out if 1.8.6  
introduced a bug of some kind whether it be in gsub or REXML.

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 11:59am UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/6 "2009-05-21T11:59:31Z")

</div>

Yukihiro M. wrote:

> The following patch should work.  
> …  
> index 2bc0042…a4a30b6 100644  
> — a/lib/rexml/text.rb  
> +++ b/lib/rexml/text.rb  
> @@ -286,7 +286,7 @@ module REXML  
> EREFERENCE = /&(?!#{Entity::NAME};)/  
> # Escapes all possible entities  
> def Text::normalize( input, doctype=nil, entity\_filter=nil )
> 
> - 
> 
> ```
> copy = input
> 
> ```
> 
> - 
> 
> ```
> copy = input.to_s
> # Doing it like this rather than in a loop improves the speed
> #copy = copy.gsub( EREFERENCE, '&amp;' )
> copy = copy.gsub( "&", "&amp;" )
> 
> ```

…which was also posted here  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/c2e2f3b9a6ffac50/95e9a6b7bc4ca00f](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/c2e2f3b9a6ffac50/95e9a6b7bc4ca00f)

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 5:19pm UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/7 "2009-05-21T17:19:40Z")

</div>

Hi,

In message “Re: gsub choking on fixnum”  
on Thu, 21 May 2009 13:30:27 +0900, Cali W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|This is a change in the REXML code, if I have to make a change, I’m more  
|comfortable with changing my own code. Is this change a REXML bug fix or  
|simply a fix for my problem? I’m still trying to figure out if 1.8.6  
|introduced a bug of some kind whether it be in gsub or REXML.

gsub has no bug. This is a backport from 1.9 rexml.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/8 "2009-05-21T17:34:35Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: gsub choking on fixnum”  
> on Thu, 21 May 2009 13:30:27 +0900, Cali W.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |This is a change in the REXML code, if I have to make a change, I’m more  
> |comfortable with changing my own code. Is this change a REXML bug fix or  
> |simply a fix for my problem? I’m still trying to figure out if 1.8.6  
> |introduced a bug of some kind whether it be in gsub or REXML.
> 
> gsub has no bug. This is a backport from 1.9 rexml.
> 
> ```
> matz.
> 
> ```

Matz,

Thank you, thank you, thank you, I really mean it, can you tell =), for  
clarifying this, I have been trying to figure out where the issue is and  
now I know. I’ll apply the patch in REXML.

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 21, 2009, 6:03pm UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/9 "2009-05-21T18:03:42Z")

</div>

Hi,

In message “Re: gsub choking on fixnum”  
on Fri, 22 May 2009 00:34:43 +0900, Cali W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Thank you, thank you, thank you, I really mean it, can you tell =), for  
|clarifying this, I have been trying to figure out where the issue is and  
|now I know. I’ll apply the patch in REXML.

FYI, I have committed the patch to the 1.8 HEAD.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![caliwildman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a6a055/32.png) [@caliwildman](https://www.ruby-forum.com/u/caliwildman)\
**Post date:** [May 22, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/gsub-choking-on-fixnum/168462/10 "2009-05-22T18:00:35Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: gsub choking on fixnum”  
> on Fri, 22 May 2009 00:34:43 +0900, Cali W.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |Thank you, thank you, thank you, I really mean it, can you tell =), for  
> |clarifying this, I have been trying to figure out where the issue is and  
> |now I know. I’ll apply the patch in REXML.
> 
> FYI, I have committed the patch to the 1.8 HEAD.
> 
> ```
> matz
> 
> ```

Matz, thanks.
