Forum: Ruby gsub choking on fixnum

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
073c197b3c34cabc15cac2421e94ec41?d=identicon&s=25 Cali Wildman (caliwildman)
on 2009-05-21 01:08
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 ...?
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2009-05-21 01:20
(Received via mailing list)
Hi,

In message "Re: gsub choking on fixnum"
    on Thu, 21 May 2009 08:08:55 +0900, Cali Wildman
<caliwildman2004-info@yahoo.com> 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.
073c197b3c34cabc15cac2421e94ec41?d=identicon&s=25 Cali Wildman (caliwildman)
on 2009-05-21 01:37
Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: gsub choking on fixnum"
>     on Thu, 21 May 2009 08:08:55 +0900, Cali Wildman
> <caliwildman2004-info@yahoo.com> 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.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2009-05-21 01:54
(Received via mailing list)
Hi,

In message "Re: gsub choking on fixnum"
    on Thu, 21 May 2009 08:37:58 +0900, Cali Wildman
<caliwildman2004-info@yahoo.com> 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 Matsumoto  <matz@ruby-lang.org>
+
+  * lib/rexml/text.rb (REXML::Text.normalize): call to_s for input.
+    [ruby-talk:337069]
+
 Mon May 18 21:40:11 2009  Tanaka Akira  <akr@fsij.org>

   * 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;" )
073c197b3c34cabc15cac2421e94ec41?d=identicon&s=25 Cali Wildman (caliwildman)
on 2009-05-21 06:30
Yukihiro Matsumoto 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 Matsumoto  <matz@ruby-lang.org>
> +
> +  * lib/rexml/text.rb (REXML::Text.normalize): call to_s for input.
> +    [ruby-talk:337069]
> +
>  Mon May 18 21:40:11 2009  Tanaka Akira  <akr@fsij.org>
>
>    * 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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-05-21 11:59
Yukihiro Matsumoto 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/brow...
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2009-05-21 17:19
(Received via mailing list)
Hi,

In message "Re: gsub choking on fixnum"
    on Thu, 21 May 2009 13:30:27 +0900, Cali Wildman
<caliwildman2004-info@yahoo.com> 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.
073c197b3c34cabc15cac2421e94ec41?d=identicon&s=25 Cali Wildman (caliwildman)
on 2009-05-21 17:34
Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: gsub choking on fixnum"
>     on Thu, 21 May 2009 13:30:27 +0900, Cali Wildman
> <caliwildman2004-info@yahoo.com> 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.
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2009-05-21 18:03
(Received via mailing list)
Hi,

In message "Re: gsub choking on fixnum"
    on Fri, 22 May 2009 00:34:43 +0900, Cali Wildman
<caliwildman2004-info@yahoo.com> 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.
073c197b3c34cabc15cac2421e94ec41?d=identicon&s=25 Cali Wildman (caliwildman)
on 2009-05-22 18:00
Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: gsub choking on fixnum"
>     on Fri, 22 May 2009 00:34:43 +0900, Cali Wildman
> <caliwildman2004-info@yahoo.com> 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.
This topic is locked and can not be replied to.