Professional Web Applications Themes

gsub with match replacement - Ruby

I'm trying to convert "12:34" => "12:34" (I know there is a CGI module with unescapeHTML(), I'm working in a ruby environment that does not allow for additions of modules) I'm trying: "12:34".gsub(/&#(\d+);/,'

'.to_i.chr) But I get "1234" Can anyone explain what I'm doing wrong? BTW I know I can do: "12:34".gsub(/&#(\d+);/) do Integer($1).chr end but for some reason in the ruby environment I'm working in I get 'Undefined method Integer for main:Object' -- James R. Leu jleu at internetnoc.com iNOC -> http://internetnoc.com/...

  1. #1

    Default gsub with match replacement

    I'm trying to convert

    "12:34" => "12:34"

    (I know there is a CGI module with unescapeHTML(), I'm working in a
    ruby environment that does not allow for additions of modules)

    I'm trying:

    "12:34".gsub(/&#(\d+);/,'\1'.to_i.chr)

    But I get "1234"

    Can anyone explain what I'm doing wrong?

    BTW I know I can do:

    "12:34".gsub(/&#(\d+);/) do
    Integer($1).chr
    end

    but for some reason in the ruby environment I'm working in
    I get 'Undefined method Integer for main:Object'


    --
    James R. Leu
    jleu at internetnoc.com
    iNOC -> http://internetnoc.com/


    James Guest

  2. #2

    Default Re: gsub with match replacement

    > I'm trying to convert 

    When ruby is about to call method #gsub, it evaluates the arguments.
    The second argument is '\1'.to_i.chr -> 0.chr -> "\000".
     

    It's very strange that you get that error (works here). Maybe you can try
    with $1.to_i.chr, as before.


    Carlos Guest

  3. #3

    Default Re: gsub with match replacement

    On Wed, Feb 04, 2004 at 02:05:44AM +0900, James R. Leu wrote: 

    That doesn't work because to_i is being called on the literal string '\1' -
    before gsub ever sees it and replaces '\1' with the matching portion of the
    string.
     

    What version of Ruby? (You can look at the constant RUBY_VERSION).
    But this should work:

    "12:34".gsub(/&#(\d+);/) { $1.to_i.chr }

    -Mark
    Mark Guest

Similar Threads

  1. gsub oddity?
    By Ralph in forum Ruby
    Replies: 7
    Last Post: January 23rd, 08:41 AM
  2. gsub question
    By Daniel in forum Ruby
    Replies: 3
    Last Post: December 17th, 07:30 PM
  3. Replies: 6
    Last Post: September 5th, 05:26 AM
  4. Replies: 8
    Last Post: August 18th, 06:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139