Marshal.dump(obj) as String bug?

Discussion in 'Ruby' started by Hampton, Nov 26, 2005.

  1. Hampton

    Hampton Guest

    Alright, so I've been scratching my head about this along with some
    folks over at #ruby-lang.

    This may be a known problem, but after spending a few minutes
    searching, I can't find any reference... so I'll document it here.

    If you take some object and marshal it, then try and concat that
    string, it produces some funny results.

    Here is the test case that I've discovered. I'm thinking this is
    happening because Marshal sends back some weird characters that are
    screwing up the string methods. But, I would still classify that as a
    bug, because when an object returns a string, that should always be a
    fairly safe string to handle or it should be returning something else.
    That would be an invalid or misformed string if you can't even use the
    regular expressions on it.

    Any help would be greatly appreciated.

    #I'm running ruby 1.8.3 on ubuntu
    #email hcatlin at gmail.com with any help


    class MyObject
    def initialize
    @name = "testvar"
    @array = Array.new
    end
    end

    test = MyObject.new
    dumpresult = Marshal.dump(test)
    puts 'Result: ' << dumpresult # Expected: "Result: MYOBJECTDUMP"
    puts 'Result=' << Marshal.dump(test) #Expected "Result=MYOBJECTDUMP"
    puts 'Result: ' + dumpresult # Expected: "Result: MYOBJECTDUMP"
    puts 'Result=' + Marshal.dump(test) #Expected "Result=MYOBJECTDUMP"
    puts "U=" << Marshal.dump(test) # Expected "U=MYOBJECTDUMP"


    #My result is this code below.

    #MyObject:
    # @array[:
    #@name"
    #testvar
    #MyObject:
    # @array[:
    #@name"
    #testvar
    #MyObject:
    # @array[:
    #@name"
    #testvar
    #MyObject:
    # @array[:
    #@name"
    #testvar
    #MyObject:
    # @array[:
    #@name"
    #testvar
     
    Hampton, Nov 26, 2005
    #1
    1. Advertising

  2. Hi --

    On Sat, 26 Nov 2005, Hampton wrote:

    > Alright, so I've been scratching my head about this along with some
    > folks over at #ruby-lang.
    >
    > This may be a known problem, but after spending a few minutes
    > searching, I can't find any reference... so I'll document it here.
    >
    > If you take some object and marshal it, then try and concat that
    > string, it produces some funny results.
    >
    > Here is the test case that I've discovered. I'm thinking this is
    > happening because Marshal sends back some weird characters that are
    > screwing up the string methods. But, I would still classify that as a
    > bug, because when an object returns a string, that should always be a
    > fairly safe string to handle or it should be returning something else.
    > That would be an invalid or misformed string if you can't even use the
    > regular expressions on it.
    >
    > Any help would be greatly appreciated.
    >
    > #I'm running ruby 1.8.3 on ubuntu
    > #email hcatlin at gmail.com with any help
    >
    >
    > class MyObject
    > def initialize
    > @name = "testvar"
    > @array = Array.new
    > end
    > end
    >
    > test = MyObject.new
    > dumpresult = Marshal.dump(test)
    > puts 'Result: ' << dumpresult # Expected: "Result: MYOBJECTDUMP"
    > puts 'Result=' << Marshal.dump(test) #Expected "Result=MYOBJECTDUMP"
    > puts 'Result: ' + dumpresult # Expected: "Result: MYOBJECTDUMP"
    > puts 'Result=' + Marshal.dump(test) #Expected "Result=MYOBJECTDUMP"
    > puts "U=" << Marshal.dump(test) # Expected "U=MYOBJECTDUMP"
    >
    >
    > #My result is this code below.
    >
    > #MyObject:
    > # @array[:
    > #@name"
    > #testvar
    > #MyObject:
    > # @array[:
    > #@name"
    > #testvar
    > #MyObject:
    > # @array[:
    > #@name"
    > #testvar
    > #MyObject:
    > # @array[:
    > #@name"
    > #testvar
    > #MyObject:
    > # @array[:
    > #@name"
    > #testvar


    If you use p instead of puts you'll see what's going on:

    "Result: \004\010o:\rMyObject\a:\v@array[\000:\n@name\"\ftestvar"

    Note the \r, which does a carriage return. Then what follows
    overwrites what came before (when you do a puts).

    You can work around this by doing:

    puts "Result:\n" << dumpresult


    David

    --
    David A. Black
     
    David A. Black, Nov 26, 2005
    #2
    1. Advertising

  3. On Nov 25, 2005, at 10:37 PM, Hampton wrote:

    > I'm thinking this is happening because Marshal sends back some
    > weird characters that are
    > screwing up the string methods. But, I would still classify that as a
    > bug, because when an object returns a string, that should always be a
    > fairly safe string to handle or it should be returning something else.


    I think of Marshal as returning binary data. In Ruby, we store that
    in a String.

    James Edward Gray II
     
    James Edward Gray II, Nov 26, 2005
    #3
  4. Hampton

    Hampton Guest

    Thanks david, that makes a lot of sense.

    Also, it makes sense as to why socket.gets was having issues with
    it.... because it was stopping at the \r.

    Thanks,
    Hampton.
     
    Hampton, Nov 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    3
    Views:
    15,696
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    503
    Peter Otten
    May 4, 2005
  3. Mike
    Replies:
    21
    Views:
    667
    Paul Rubin
    Jan 15, 2006
  4. Replies:
    10
    Views:
    532
    Aaron Watters
    Jun 18, 2008
  5. Michael Davis

    Ruby 1.8 and Marshal.load/Marshal.dump

    Michael Davis, Oct 10, 2003, in forum: Ruby
    Replies:
    0
    Views:
    171
    Michael Davis
    Oct 10, 2003
Loading...

Share This Page