Gsub choking on fixnum

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 …?

Hi,

In message “Re: gsub choking on fixnum”
on Thu, 21 May 2009 08:08:55 +0900, Cali W.
[email protected] 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.

Yukihiro M. wrote:

Hi,

In message “Re: gsub choking on fixnum”
on Thu, 21 May 2009 08:08:55 +0900, Cali W.
[email protected] 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.

Hi,

In message “Re: gsub choking on fixnum”
on Thu, 21 May 2009 08:37:58 +0900, Cali W.
[email protected] 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. [email protected]
+

    • lib/rexml/text.rb (REXML::Text.normalize): call to_s for input.
  • [ruby-talk:337069]

Mon May 18 21:40:11 2009 Tanaka A. [email protected]

  • 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, '&' )
     copy = copy.gsub( "&", "&" )
    

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. [email protected]
+

    • lib/rexml/text.rb (REXML::Text.normalize): call to_s for input.
  • [ruby-talk:337069]

Mon May 18 21:40:11 2009 Tanaka A. [email protected]

  • 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, '&' )
     copy = copy.gsub( "&", "&" )
    

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.

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, '&' )
     copy = copy.gsub( "&", "&" )
    

…which was also posted here
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/c2e2f3b9a6ffac50/95e9a6b7bc4ca00f

Hi,

In message “Re: gsub choking on fixnum”
on Thu, 21 May 2009 13:30:27 +0900, Cali W.
[email protected] 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.

Yukihiro M. wrote:

Hi,

In message “Re: gsub choking on fixnum”
on Thu, 21 May 2009 13:30:27 +0900, Cali W.
[email protected] 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.

Hi,

In message “Re: gsub choking on fixnum”
on Fri, 22 May 2009 00:34:43 +0900, Cali W.
[email protected] 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.

Yukihiro M. wrote:

Hi,

In message “Re: gsub choking on fixnum”
on Fri, 22 May 2009 00:34:43 +0900, Cali W.
[email protected] 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.