Implemented optional buffer argument for Array#pack #4502
Conversation
public RubyString pack(ThreadContext context, IRubyObject obj, IRubyObject maybeOpts) { | ||
Ruby runtime = context.runtime; | ||
IRubyObject opts = ArgsUtil.getOptionsArg(runtime, maybeOpts); | ||
RubyString str = RubyString.newEmptyString(runtime); |
kares
Feb 24, 2017
Member
preferably use RubyString.newString()
the newEmptyString is for cases such as return ''
preferably use RubyString.newString()
the newEmptyString is for cases such as return ''
if (!opts.isNil()){ | ||
IRubyObject buffer = ((RubyHash) opts).fastARef(runtime.newSymbol("buffer")); | ||
if (buffer != null) { | ||
str = (RubyString) buffer; |
kares
Feb 24, 2017
Member
maybe the RubyString
allocation (above) can be delayed until its sure a buffer is not provided?
maybe the RubyString
allocation (above) can be delayed until its sure a buffer is not provided?
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw concurrentModification(context.runtime, e); | ||
} catch (ClassCastException e){ |
kares
Feb 25, 2017
Member
why is this introduced here? is it due the obj.convertToString()
removal? (would keep it than)
why is this introduced here? is it due the obj.convertToString()
removal? (would keep it than)
whwilder
Feb 25, 2017
Author
Contributor
In my original implementation I hadn't considered what would happen if the buffer were nil or some other class besides String. MRI Ruby throws a type error, and this seemed to be the most straightforward way of mirroring that behavior.
In my original implementation I hadn't considered what would happen if the buffer were nil or some other class besides String. MRI Ruby throws a type error, and this seemed to be the most straightforward way of mirroring that behavior.
kares
Feb 25, 2017
Member
buffer.convertToString()
will do that for you - raising a TypeError
e.g. on nil
... ClassCastException
isn't very bad but theoretically might happen from some other place
if you could update to get rid of it that would be great ... the rest of the code is great merge material!
buffer.convertToString()
will do that for you - raising a TypeError
e.g. on nil
... ClassCastException
isn't very bad but theoretically might happen from some other place
if you could update to get rid of it that would be great ... the rest of the code is great merge material!
ref #4293