Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String#hex confusion

Reply
Thread Tools

String#hex confusion

 
 
Steven Jenkins
Guest
Posts: n/a
 
      02-20-2004
$ irb x
x(main):001:0> "This works"
=> "This works"
x(main):002:0> '22'
=> "22"
x(main):003:0> '22'.class
=> String
x(main):004:0> '22'.hex.to_s
=> "34"
x(main):005:0> "Why doesn't this?"
=> "Why doesn't this?"
x(main):006:0> '22'.sub(/(\d\d)/, "#{'\1'}")
=> "22"
x(main):007:0> '22'.sub(/(\d\d)/, "#{'\1'.class}")
=> "String"
x(main):008:0> '22'.sub(/(\d\d)/, "#{'\1'.hex.to_s}")
=> "0"

I understand everything but the last line. What am I missing?

Steve


 
Reply With Quote
 
 
 
 
Eric Sunshine
Guest
Posts: n/a
 
      02-20-2004
On Sat, 21 Feb 2004 04:31:07 +0900, Steven Jenkins wrote:
> x(main):008:0> '22'.sub(/(\d\d)/, "#{'\1'.hex.to_s}")
> => "0"
> I understand everything but the last line. What am I missing?


You are applying 'hex' to the literal string "\1". Since '\' is not a hex
digit, it returns 0. What you really want is:

'22'.sub(/(\d\d)/) { "#{$1.hex.to_s}" }

-- ES


 
Reply With Quote
 
 
 
 
Mark Hubbart
Guest
Posts: n/a
 
      02-20-2004

On Feb 20, 2004, at 11:31 AM, Steven Jenkins wrote:

> x(main):005:0> "Why doesn't this?"
> => "Why doesn't this?"
> x(main):006:0> '22'.sub(/(\d\d)/, "#{'\1'}")
> => "22"
> x(main):007:0> '22'.sub(/(\d\d)/, "#{'\1'.class}")
> => "String"
> x(main):008:0> '22'.sub(/(\d\d)/, "#{'\1'.hex.to_s}")
> => "0"
>
> I understand everything but the last line. What am I missing?


try this:

irb(main):001:0> '22'.sub(/(\d\d)/, "#{'\1'.inspect}")
=> "\"\\1\""

whoa! in your example, lines 007 and 008, you are calling the #class
and #hex methods on the string literal '\1', not the result of the
substitution.

Instead, you may want:

irb(main):002:0> '22'.sub(/(\d\d)/){$1.inspect}
=> "\"22\""
irb(main):003:0> '22'.sub(/(\d\d)/){$1.hex.to_s}
=> "34"

If you pass a block to #sub or #gsub, it passes the block and evaluates
it each time it finds a match, whereas passing a replacement string as
an argument to those methods will evaluate the string once. So any time
you want to use code in #sub or #gsub, always pass a block, or you
might get strange results.

-Mark



 
Reply With Quote
 
Steven Jenkins
Guest
Posts: n/a
 
      02-20-2004
Mark Hubbart wrote:

> whoa! in your example, lines 007 and 008, you are calling the #class and
> #hex methods on the string literal '\1', not the result of the
> substitution.


OK, that makes sense. '\1'.class == '22'.class. Thanks, Mark (& Eric).

Steve


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mozilla & Firefox Confusion listsubscriber@hotmail.com Firefox 9 01-20-2005 05:09 PM
Confusion about location of Mozilla files (Mandrake Linux 10.0) Hallvard Tangeraas Firefox 0 09-14-2004 09:46 AM
Procedures in testbench confusion Peter Hermansson VHDL 2 08-25-2004 02:15 PM
confusion when resetting registers martin f. krafft VHDL 2 08-19-2004 06:29 AM
defined() confusion Chris Perl 2 12-18-2003 06:06 PM



Advertisments
 



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