Fix object writing and reading for Ruby 1.8 IO differences.
This commit is contained in:
parent
70b372136d
commit
a21071b590
|
@ -60,6 +60,18 @@ module ProcessShared
|
|||
#
|
||||
# Raises IndexError if there is insufficient space.
|
||||
def put_object(offset, obj)
|
||||
# FIXME: This is a workaround to an issue I'm seeing in
|
||||
# 1.8.7-p352 (not tested in other 1.8's). If I used the code
|
||||
# below that works in 1.9, then inside SharedMemoryIO#write, the
|
||||
# passed string object is 'terminated' (garbage collected?) and
|
||||
# won't respond to any methods... This way is less efficient
|
||||
# since it involves the creation of an intermediate string, but
|
||||
# it works in 1.8.7-p352.
|
||||
if RUBY_VERSION =~ /^1.8/
|
||||
str = Marshal.dump(obj)
|
||||
return put_bytes(offset, str, 0, str.size)
|
||||
end
|
||||
|
||||
io = SharedMemoryIO.new(self)
|
||||
io.seek(offset)
|
||||
Marshal.dump(obj, io)
|
||||
|
@ -77,7 +89,7 @@ module ProcessShared
|
|||
|
||||
# Equivalent to {#put_object(0, obj)}
|
||||
def write_object(obj)
|
||||
Marshal.dump(obj, to_shm_io)
|
||||
put_object(0, obj)
|
||||
end
|
||||
|
||||
# Equivalent to {#read_object(0, obj)}
|
||||
|
|
|
@ -126,8 +126,20 @@ module ProcessShared
|
|||
_getbyte
|
||||
end
|
||||
|
||||
def getc
|
||||
raise NotImplementedError
|
||||
# {#getc} in Ruby 1.9 returns String or nil. In 1.8, it returned
|
||||
# Fixnum of nil (identical to getbyte).
|
||||
#
|
||||
# FIXME: should this be encoding/character aware?
|
||||
def getc19
|
||||
if b = getbyte
|
||||
'' << b
|
||||
end
|
||||
end
|
||||
# FIXME: ignores versions prior to 1.8.
|
||||
if RUBY_VERSION =~ /^1.8/
|
||||
alias_method :getc, :getbyte
|
||||
else
|
||||
alias_method :getc, :getc19
|
||||
end
|
||||
|
||||
def gets
|
||||
|
|
Loading…
Reference in New Issue