[MacRuby] #732: Ruby strings should have a to_data method
#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/>
#732: Ruby strings should have a to_data method -------------------------------+-------------------------------------------- Reporter: me@… | Owner: lsansonetti@… Type: enhancement | Status: closed Priority: minor | Milestone: MacRuby 0.7 Component: MacRuby | Resolution: fixed Keywords: string encoding | -------------------------------+-------------------------------------------- Changes (by lsansonetti@…): * status: new => closed * resolution: => fixed * milestone: => MacRuby 0.7 Comment: Should be fixed in r4189. All strings now respond to #to_data which returns an NSData wrapping the internal bytes storage (according to the current encoding). The method will raise an exception in case the receiver is a pure NSString. -- Ticket URL: <http://www.macruby.org/trac/ticket/732#comment:1> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby