Project

General

Profile

Feature #17834

Updated by dsisnero (Dominic Sisneros) over 3 years ago

"this is a string".bytes should == Bytes.new('this is a string')    # an immutable bytes object that implements memory view 

 ``` ruby 
 bytestring = Bytes.new('this is a string') 

 mv = Fiddle::MemoryView.new(bytestring) 
 mv[0] # 116 
 ``` 



 

 similary String.new('this is a string').bytes    == ByteArray.new('this is a string')    #    a mutable byte array object that implements memoryview , takes an optional encoding that uses current string encoding 

 ``` ruby 
 ba = ByteArray.new('this is a string') 
 mv = Fiddle::MemoryView.new(ba) 
 mv[0] # 116 
 mv[0] = 120 # memoryview works on underlying object - changes mv.obj 
 ba.pack('C*').force_encoding('UTF-8') # 'xhis is a string' 

 ``` 

 also it would be good if String#bytes returns the new classes - (return something that implements memoryview) not just plain array 
 or add a new %b literal that returns an immutable ByteString %b('this is a string') 

Back