Fix object writing and reading for Ruby 1.8 IO differences.

This commit is contained in:
Patrick Mahoney 2011-12-21 07:58:55 -06:00
parent 70b372136d
commit a21071b590
2 changed files with 27 additions and 3 deletions

View File

@ -60,6 +60,18 @@ module ProcessShared
# #
# Raises IndexError if there is insufficient space. # Raises IndexError if there is insufficient space.
def put_object(offset, obj) 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 = SharedMemoryIO.new(self)
io.seek(offset) io.seek(offset)
Marshal.dump(obj, io) Marshal.dump(obj, io)
@ -77,7 +89,7 @@ module ProcessShared
# Equivalent to {#put_object(0, obj)} # Equivalent to {#put_object(0, obj)}
def write_object(obj) def write_object(obj)
Marshal.dump(obj, to_shm_io) put_object(0, obj)
end end
# Equivalent to {#read_object(0, obj)} # Equivalent to {#read_object(0, obj)}

View File

@ -126,8 +126,20 @@ module ProcessShared
_getbyte _getbyte
end end
def getc # {#getc} in Ruby 1.9 returns String or nil. In 1.8, it returned
raise NotImplementedError # 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 end
def gets def gets