[MacRuby-devel] [MacRuby] #732: Ruby strings should have a to_data method
MacRuby
ruby-noreply at macosforge.org
Fri May 28 14:26:07 PDT 2010
#732: Ruby strings should have a to_data method
-------------------------------+--------------------------------------------
Reporter: me@… | Owner: lsansonetti@…
Type: enhancement | Status: new
Priority: minor | Milestone:
Component: MacRuby | Keywords: string encoding
-------------------------------+--------------------------------------------
From my mailing list post:
=======
Hello,
I've been playing around with macruby a bunch and I hit a point of
confusion. I basically did "....".pack("H*") which returns a ruby
string. This string is an ASCII-8BIT encoded ruby string which is an
alias for a Binary string (aka, it has no encoding). IMO, this should
really be backed by an NSData class as opposed to an NSString class
because it doesn't make much sense. In fact, it's pretty much
impossible to get any useful information out of an ASCII-8BIT encoded
ruby string from the objective-c side of things.
To get around this, I did the following:
class String
def to_data
return NSData.data unless length > 0
bytes = self.bytes.to_a
p = Pointer.new_with_type("char *", bytes.length)
bytes.each_with_index do |char, i|
p[i] = char
end
NSData.dataWithBytes(p, length:bytes.length)
end
end
I'm not sure what the solution to this is, but I thought I'd bring up
the issue on the mailing list.
=======
--
Ticket URL: <http://www.macruby.org/trac/ticket/732>
MacRuby <http://macruby.org/>
More information about the MacRuby-devel
mailing list