Forum: Ruby Re: Write Out Then Read In Hash of Two-element Arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-04-20 11:37
(Received via mailing list)
Well,

your primary problem is that you write both elements of the
arrays on separat lines. When reading you will read more
lines than you expect (twice as much to be correct).

So an easy fix would be to write using

  fileOut.puts histHash[i].join(',')

so both values are on the same line.
If you read them in you have to split the line to get two
distinct values again.

  fileIn.each_line{|line|histHash[i]= line.split(',').map{|s| s.to_i}}

that said, I would recommend having a close look at yaml.

cheers

Simon
Efe70cff891d5722448a6512cfd1e1f1?d=identicon&s=25 David Bailey (dbailey7)
on 2006-04-20 17:29
Kroeger, Simon (ext) wrote:
> Well,
>
> your primary problem is that you write both elements of the
> arrays on separat lines. When reading you will read more
> lines than you expect (twice as much to be correct).
>
> So an easy fix would be to write using
>
>   fileOut.puts histHash[i].join(',')
>
> so both values are on the same line.
> If you read them in you have to split the line to get two
> distinct values again.
>
>   fileIn.each_line{|line|histHash[i]= line.split(',').map{|s| s.to_i}}
>
> that said, I would recommend having a close look at yaml.
>
> cheers
>
> Simon

Simon,

Thank you.  Your suggestion, of course, worked.  Why I oddly thought
Ruby should split the hash elements into separate lines, but not the its
array elements, I'll never know! After being up all night, I guess my
brain just stopped working.

I've seen the acronym yaml around a few times, but short a guessing that
it means yet another meta language I do not know what it actually does,
but I will take a look at it as you suggest.

Again, thank you.  I'm awake now!  (:>)

David
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-04-20 17:42
(Received via mailing list)
On Fri, 21 Apr 2006, David Bailey wrote:

>
> David

it's a serialization format, not a metalanguage.  the thing is it's a
__readable__ one:

     harp:~ > cat a.rb
     require 'yaml'

     hash = {
       "a" => 0,
       "b" => 1,
       "c" => 2,
     }

     open('hash','w'){|f| f.write hash.to_yaml}
     open('hash'){|f| print f.read}
     hash = open('hash'){|f| YAML::load f}
     p hash


     harp:~ > ruby a.rb
     ---
     a: 0
     b: 1
     c: 2
     {"a"=>0, "b"=>1, "c"=>2}

regards.

-a
Efe70cff891d5722448a6512cfd1e1f1?d=identicon&s=25 David Bailey (dbailey7)
on 2006-04-20 18:29
David Bailey wrote:
> Kroeger, Simon (ext) wrote:
>>>>
>> that said, I would recommend having a close look at yaml.
>>
>> cheers
>>
>> Simon
>
> Simon,
>
> Thank you.  Your suggestion, of course, worked.  Why I oddly thought
> Ruby should split the hash elements into separate lines, but not the its
> array elements, I'll never know! After being up all night, I guess my
> brain just stopped working.
>
> I've seen the acronym yaml around a few times, but short a guessing that
> it means yet another meta language I do not know what it actually does,
> but I will take a look at it as you suggest.
>
> Again, thank you.  I'm awake now!  (:>)
>
> David


Uh, I stand corrected.  I do actually recall reading about yaml.  It was
covered in Programming Ruby by Dave Thomas and the pragmatic
programmers.  I guess I just forgot.  God, I hate when that happens!
(:>)
Efe70cff891d5722448a6512cfd1e1f1?d=identicon&s=25 David Bailey (dbailey7)
on 2006-04-20 18:33
unknown wrote:
> On Fri, 21 Apr 2006, David Bailey wrote:
>
>>
>> David
>
> it's a serialization format, not a metalanguage.  the thing is it's a
> __readable__ one:
>
>      harp:~ > cat a.rb
>      require 'yaml'
>
>      hash = {
>        "a" => 0,
>        "b" => 1,
>        "c" => 2,
>      }
>
>      open('hash','w'){|f| f.write hash.to_yaml}
>      open('hash'){|f| print f.read}
>      hash = open('hash'){|f| YAML::load f}
>      p hash
>
>
>      harp:~ > ruby a.rb
>      ---
>      a: 0
>      b: 1
>      c: 2
>      {"a"=>0, "b"=>1, "c"=>2}
>
> regards.
>
> -a

Yes, thank you for jogging my memory!  Now that yo mention it, I do
recall reading about yaml in the pickaxe book.  It slipped off the edge
of my mind, I guess.  Geeze, I dislike that forgetfulness thing ...
(:>)
This topic is locked and can not be replied to.