From a21071b5904030472069cffd6c8115667940aaa3 Mon Sep 17 00:00:00 2001 From: Patrick Mahoney Date: Wed, 21 Dec 2011 07:58:55 -0600 Subject: [PATCH] Fix object writing and reading for Ruby 1.8 IO differences. --- lib/process_shared/shared_memory.rb | 14 +++++++++++++- lib/process_shared/shared_memory_io.rb | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/process_shared/shared_memory.rb b/lib/process_shared/shared_memory.rb index fe0cd3f..7960b06 100644 --- a/lib/process_shared/shared_memory.rb +++ b/lib/process_shared/shared_memory.rb @@ -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)} diff --git a/lib/process_shared/shared_memory_io.rb b/lib/process_shared/shared_memory_io.rb index 1164575..a21a8a0 100644 --- a/lib/process_shared/shared_memory_io.rb +++ b/lib/process_shared/shared_memory_io.rb @@ -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