From: Joshua Ballanco Date: 2011-10-23T11:16:26+09:00 Subject: [ruby-core:40286] Re: Counseling --4ea378f5_327b517e_1124 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline On Saturday, October 22, 2011 at 12:43 PM, Jon wrote: > > > What Ruby needs (IMHO), is the equivalent of Obj-C's NSData class. That is, something which can hold a contiguous span of raw bytes without encoding, but with the ability to access ranges and iterate over the data like a String. I regret that I did not recall this desire of mine for the original Ruby 2.0 feature list (I originally encountered the need for this when writing the ControlTower server for MacRuby; which, consequently, does make use of NSData). I would, however, like to propose such a class for Ruby 2.0. > > > > > > > > > > > > What's your view regarding both the `bytes` (immutable) and `bytearray` (mutable) abstractions from > > http://docs.python.org/py3k/library/functions.html#bytearray > > > Yes, this sounds like a very similar idea (NSData is immutable and has an NSMutableData counterpart). I think the intro for the NSData documentation captures the motivation perfectly: > NSData and its mutable subclass NSMutableData provide data objects, object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects. Basically, since the Array class in Ruby is designed to hold objects, there is an annoying amount of overhead required to use Ruby arrays to hold simple bytes (e.g. you have to manually decompose bytes on each append operation). On the other hand, since Ruby does its best to always do the right thing with encodings for String objects, it can get annoying to try and use Ruby strings to hold bytes (you never know when your BINARY string might be coerced into UTF-8). --4ea378f5_327b517e_1124 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
On Saturday, October 22, 2011 at 12:43 PM, Jon= wrote:
 
What Ruby needs (IMHO), is the equivalent of Obj-C's NSData class. That i= s, something which can hold a contiguous span of raw bytes without encodi= ng, but with the ability to access ranges and iterate over the data like = a String. I regret that I did not recall this desire of mine for the orig= inal Ruby 2.0 feature list (I originally encountered the need for this wh= en writing the ControlTower server for MacRuby; which, consequently, does= make use of NSData). I would, however, like to propose such a class for = Ruby 2.0.
 
&nbs= p;
What's your view regarding both the =60bytes=60 (i= mmutable) and =60bytearray=60 (mutable) abstractions from
 = ;


Yes, this sounds like a very si= milar idea (NSData is immutable and has an NSMutableData counterpart). I = think the intro for the NSData documentation captures the motivation perf= ectly:

NSData and its mutable subclass NSMutableData provide data objects, object-oriented wrapper= s for byte buffers. Data objects let simple allocated buffers (that is, d= ata with no embedded pointers) take on the behavior of =46oundation objec= ts.

Basically, since the Array cla= ss in Ruby is designed to hold objects, there is an annoying amount of ov= erhead required to use Ruby arrays to hold simple bytes (e.g. you have to= manually decompose bytes on each append operation). On the other hand, s= ince Ruby does its best to always do the right thing with encodings for S= tring objects, it can get annoying to try and use Ruby strings to hold by= tes (you never know when your BINARY string might be coerced into UT=46-8= ). 
--4ea378f5_327b517e_1124--