[newbie]saving and reading array of associative array

Discussion in 'Ruby' started by Yvon Thoraval, Sep 17, 2003.

  1. i'm looking for examples of saving to file and reading back an array of
    associative array, in a ruby like way.

    saying i have something like :

    one = {"Name" => "Smith", "Surname" => "John" ...}
    two = {"Name" => "Dupont", "Surname" => "Jean" ...}

    myFriends = [one, two, ...]

    i want to save into a file "myFriends.db" the array of hashes myFriends
    in such a way reading it back will recorver the array of hashes the
    easiest way...
    --
    Yvon
     
    Yvon Thoraval, Sep 17, 2003
    #1
    1. Advertising

  2. il Wed, 17 Sep 2003 11:13:26 +0200,
    d (Yvon Thoraval) ha
    scritto::

    >i'm looking for examples of saving to file and reading back an array of
    >associative array, in a ruby like way.
    >
    >saying i have something like :
    >
    >one = {"Name" => "Smith", "Surname" => "John" ...}
    >two = {"Name" => "Dupont", "Surname" => "Jean" ...}
    >
    >myFriends = [one, two, ...]
    >
    >i want to save into a file "myFriends.db" the array of hashes myFriends
    >in such a way reading it back will recorver the array of hashes the
    >easiest way...


    ----------------------------------------------------------
    Marshal::dump
    dump( anObject [, anIO] , limit=--1 ) -> anIO
    ------------------------------------------------------------------------
    Serializes anObject and all descendent objects. If anIO is
    specified, the serialized data will be written to it, otherwise
    the
    data will be returned as a String. If limit is specified, the
    traversal of subobjects will be limited to that depth. If limit
    is
    negative, no checking of depth will be performed.

    More easy than you think ;)

    BTW you could even use PStore, included in the standard lib, it works
    similar to an hash, but gets saved on disk and supports 'transactions'
    for data integrity.
     
    gabriele renzi, Sep 17, 2003
    #2
    1. Advertising

  3. gabriele renzi <> wrote:

    >
    > ----------------------------------------------------------
    > Marshal::dump
    > dump( anObject [, anIO] , limit=--1 ) -> anIO
    > ------------------------------------------------------------------------
    > Serializes anObject and all descendent objects. If anIO is
    > specified, the serialized data will be written to it, otherwise
    > the
    > data will be returned as a String. If limit is specified, the
    > traversal of subobjects will be limited to that depth. If limit
    > is
    > negative, no checking of depth will be performed.
    >
    > More easy than you think ;)
    >
    > BTW you could even use PStore, included in the standard lib, it works
    > similar to an hash, but gets saved on disk and supports 'transactions'
    > for data integrity.


    tanxs a lot !
    --
    Yvon
     
    Yvon Thoraval, Sep 17, 2003
    #3
  4. Yvon Thoraval

    Joey Gibson Guest

    On 9/17/2003 6:34 AM, gabriele renzi wrote:

    >Marshal::dump
    > dump( anObject [, anIO] , limit=--1 ) -> anIO
    >
    >BTW you could even use PStore, included in the standard lib, it works
    >similar to an hash, but gets saved on disk and supports 'transactions'
    >for data integrity.
    >
    >


    You could also use YAML:

    require 'yaml'
    one = {"Name" => "Smith", "Surname" => "John"}
    two = {"Name" => "Dupont", "Surname" => "Jean"}

    myFriends = [one, two]

    File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)}
    myFriends = File.open("myfriends.db") {|file| YAML.load(file)}


    --
    Dean saor, dean saor an spiorad. Is seinn d'orain beo.

    http://www.joeygibson.com
    http://www.joeygibson.com/blog/life/Wisdom.html
     
    Joey Gibson, Sep 17, 2003
    #4
  5. Joey Gibson <> wrote:

    >
    > You could also use YAML:
    >
    > require 'yaml'
    > one = {"Name" => "Smith", "Surname" => "John"}
    > two = {"Name" => "Dupont", "Surname" => "Jean"}
    >
    > myFriends = [one, two]
    >
    > File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)}
    > myFriends = File.open("myfriends.db") {|file| YAML.load(file)}


    fine, i should say i've other versions of what i plan to do in :

    - perlCocoa (CamelBones specific to MacOS X)
    - php + xml
    - php + mySQL

    --
    Yvon
     
    Yvon Thoraval, Sep 17, 2003
    #5
  6. On Wed, 17 Sep 2003 21:22:13 +0900
    Joey Gibson <> wrote:

    > On 9/17/2003 6:34 AM, gabriele renzi wrote:
    >
    > >Marshal::dump
    > > dump( anObject [, anIO] , limit=--1 ) -> anIO
    > >
    > >BTW you could even use PStore, included in the standard lib, it works
    > >similar to an hash, but gets saved on disk and supports 'transactions'
    > >for data integrity.
    > >
    > >

    >
    > You could also use YAML:
    >
    > require 'yaml'
    > one = {"Name" => "Smith", "Surname" => "John"}
    > two = {"Name" => "Dupont", "Surname" => "Jean"}
    >
    > myFriends = [one, two]
    >
    > File.open("myfriends.db", "w") {|file| file.write(myFriends.to_yaml)}
    > myFriends = File.open("myfriends.db") {|file| YAML.load(file)}


    Yes, I was just about to mention YAML. Also note that you can use
    YAML::Store as a drop-in PStore replacement. (In fact, this example is
    the example given in pstore.rb, I just require'd yaml/store, and changed
    the db = line to YAML::Store)

    ~/prog/ruby$ cat yaml-store-example.rb
    require 'yaml/store'
    db = YAML::Store.new("/tmp/foo")
    db.transaction do
    p db.roots
    ary = db["root"] = [1,2,3,4]
    ary[0] = [1,1.5]
    end

    db.transaction do
    p db["root"]
    end
    ~/prog/ruby$ ruby yaml-store-example.rb
    []
    [[1, 1.5], 2, 3, 4]
    ~/prog/ruby$ cat /tmp/foo
    ---
    root:
    -
    - 1
    - 1.5
    - 2
    - 3
    - 4
    ~/prog/ruby$

    Jason Creighton
     
    Jason Creighton, Sep 17, 2003
    #6
    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. Frank Fredstone
    Replies:
    1
    Views:
    411
    =?ISO-8859-1?Q?Tobias_Schr=F6er?=
    Sep 19, 2006
  2. desktop
    Replies:
    5
    Views:
    395
    James Kanze
    Jun 26, 2007
  3. Little Monster
    Replies:
    4
    Views:
    159
    Little Monster
    May 8, 2004
  4. VK
    Replies:
    47
    Views:
    563
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    664
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page