marshalling and serialiing to IO problem

Discussion in 'Ruby' started by Junkone, Jun 5, 2008.

  1. Junkone

    Junkone Guest

    hello
    i am trying to serialise a object to file. i have errors when i try to
    restore the object. any ideas

    for eg
    irb(main):009:0> a=Hash.new
    => {}
    irb(main):010:0> a[1]='key'
    => "key"
    irb(main):011:0> a[1]='kew'
    => "kew"
    irb(main):016:0> a=> {1=>"kew"}
    irb(main):022:0> f=File.new("c:\\temp\\log",File::CREAT|File::TRUNC|
    File::RDWR)
    => #<File:c:\temp\log>
    irb(main):023:0> data=Marshal.dump(a,f)
    => #<File:c:\temp\log>
    irb(main):024:0> f.close
    => nil
    irb(main):025:0> f1=File.open("c:\\temp\\log",File::NONBLOCK|
    File::RDONLY)
    => #<File:c:\temp\log>
    irb(main):026:0> Z=Marshal.load(f1)
    IOError: not opened for reading from (irb):26:in `getc'
    from (irb):26:in `load'
    from (irb):26
     
    Junkone, Jun 5, 2008
    #1
    1. Advertising

  2. On 05.06.2008 20:34, Junkone wrote:
    > hello
    > i am trying to serialise a object to file. i have errors when i try to
    > restore the object. any ideas
    >
    > for eg
    > irb(main):009:0> a=Hash.new
    > => {}
    > irb(main):010:0> a[1]='key'
    > => "key"
    > irb(main):011:0> a[1]='kew'
    > => "kew"
    > irb(main):016:0> a=> {1=>"kew"}
    > irb(main):022:0> f=File.new("c:\\temp\\log",File::CREAT|File::TRUNC|
    > File::RDWR)
    > => #<File:c:\temp\log>
    > irb(main):023:0> data=Marshal.dump(a,f)
    > => #<File:c:\temp\log>
    > irb(main):024:0> f.close
    > => nil
    > irb(main):025:0> f1=File.open("c:\\temp\\log",File::NONBLOCK|
    > File::RDONLY)
    > => #<File:c:\temp\log>
    > irb(main):026:0> Z=Marshal.load(f1)
    > IOError: not opened for reading from (irb):26:in `getc'
    > from (irb):26:in `load'
    > from (irb):26


    Works for me. Why do you open in nonblocking mode? That might cause
    issues on your system and it's not needed. Also, why do you open in RW
    mode when you want to write only?

    Here's what I usually do

    irb(main):001:0> a={1=>2}
    => {1=>2}
    irb(main):002:0> File.open("foo","wb") {|io| Marshal.dump(a,io)}
    => #<File:foo (closed)>

    irb(main):007:0> File.open("foo","rb") {|io| Marshal.load io}
    => {1=>2}

    If you prefer boolean flags:

    irb(main):009:0> File.open("foo", File::WRONLY | File::BINARY) {|io|
    Marshal.dump(a,io)}
    => #<File:foo (closed)>
    irb(main):010:0> File.open("foo", File::RDONLY | File::BINARY) {|io|
    Marshal.load io}
    => {1=>2}

    Cheers

    robert
     
    Robert Klemme, Jun 5, 2008
    #2
    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. Replies:
    11
    Views:
    489
  2. d-42
    Replies:
    1
    Views:
    601
  3. Brian
    Replies:
    17
    Views:
    614
    Brian
    Dec 25, 2009
  4. Charles Hixson

    Singletons and Marshalling

    Charles Hixson, Nov 14, 2003, in forum: Ruby
    Replies:
    0
    Views:
    106
    Charles Hixson
    Nov 14, 2003
  5. Hampton

    Marshalling problem

    Hampton, Nov 26, 2005, in forum: Ruby
    Replies:
    1
    Views:
    113
    David A. Black
    Nov 26, 2005
Loading...

Share This Page