[MacRuby-devel] [MacRuby] #769: String::ord with packed array in MacRuby
MacRuby
ruby-noreply at macosforge.org
Wed Jul 7 17:54:53 PDT 2010
#769: String::ord with packed array in MacRuby
---------------------------------+------------------------------------------
Reporter: babs.devs@… | Owner: lsansonetti@…
Type: defect | Status: new
Priority: blocker | Milestone: MacRuby 0.7
Component: MacRuby | Keywords: binary, pack, ord, ordinal, String, String::ord
---------------------------------+------------------------------------------
Comment(by babs.devs@…):
Replying to [ticket:769 babs.devs@…]:
> When attempting to get the ordinal from a character of an array packed
to a binary string, MacRuby returns inconsistent values, and sometimes
seg. faults.
>
> {{{
> sha1 = ["94f389bf5d9af4511597d035e69d1be9510b50c7"].pack("H*")
> sha1[0].ord
> #Macirb has returned:
> # 63, 96, 65428(most often), 65535, and seg. faults
> #Irb returns: 148
>
> foo = ['bar'].pack('H*')
> foo[1].ord
> #Macirb:65456 Irb:176
>
> "cat"[0].ord
> #Works as expected
> }}}
>
> Tested with Ruby 1.9.1 and Macruby 6/23/10 nightly.
It looks like this might just be a fundamental misunderstanding on my part
about how Ruby 1.9 vs MacRuby handle encoding. But I don't know enough to
say one way or the other.
{{{
foo = ["bar"].pack("H*")
first_char = foo[0]
p first_char
#Macruby:"\xBA" Ruby:"\xBA"
p first_char.encoding
#Macruby:<Encoding:ASCII-8BIT> Ruby:<Encoding:ASCII-8BIT>
p first_char.ord
#Macruby: 65466 Ruby:186
first_char.each_codepoint {|cp| p cp}
#Macruby: 65466 Ruby:186
first_char.each_char {|ch| p ch}
#Macruby: "ᄎ" Ruby:"\xBA"
}}}
I would expect that since they the same encoding (& bytesize is the same),
the last line would output "\xBA" in both cases. Is that an incorrect
assumption?
I've been trying to wrap my head around the encoding stuff, and possible
discrepancies between 1.9 and MacRuby, so any help is much appreciated.
Thanks,
Babs
*Tested with Ruby 1.9.2 and 07/07/10 Nightly MacRuby
--
Ticket URL: <http://www.macruby.org/trac/ticket/769#comment:1>
MacRuby <http://macruby.org/>
More information about the MacRuby-devel
mailing list