Problem migrating to Ruby 1.9.2

Some assistance please - I have a Ruby project, which I started in Ruby
1.8.6/7, some time ago. It involves several custom data classes, which
can have up to 500 multi-field items in “lists”. The size of the data
means that file storage is crucial, however, with the massive changes
made to yaml.rb in Ruby 1.9.n, I find that, although I can load existing
data from file, I can’t find a way to save it back to file. Anyone got
any suggestions where to start?
I’d prefer using a proper database but can’t get any of those to behave
properly either!!!

On Feb 3, 2012, at 14:02 , Patrick B. wrote:

Some assistance please - I have a Ruby project, which I started in Ruby
1.8.6/7, some time ago. It involves several custom data classes, which
can have up to 500 multi-field items in “lists”. The size of the data
means that file storage is crucial, however, with the massive changes
made to yaml.rb in Ruby 1.9.n, I find that, although I can load existing
data from file, I can’t find a way to save it back to file. Anyone got
any suggestions where to start?

It should work just the same. You need to show us some code and the
actual error message and backtrace for us to be able to help.

On Fri, Feb 3, 2012 at 11:02 PM, Patrick B. [email protected]
wrote:

Some assistance please - I have a Ruby project, which I started in Ruby
1.8.6/7, some time ago. It involves several custom data classes, which
can have up to 500 multi-field items in “lists”. The size of the data
means that file storage is crucial, however, with the massive changes
made to yaml.rb in Ruby 1.9.n, I find that, although I can load existing
data from file, I can’t find a way to save it back to file. Anyone got
any suggestions where to start?

Did you consider using Marshal?

I’d prefer using a proper database but can’t get any of those to behave
properly either!!!

What does that mean?

Kind regards

robert

Just went back to it to get a copy of the errors, which were extensive,
and all related to the YAML module, and it is now working correctly. It
has been throwing errors since Wednesday, when I updated Ruby!!!
Thanks all.

On Fri, Feb 3, 2012 at 18:02, Patrick B. [email protected]
wrote:

Just went back to it to get a copy of the errors, which were extensive,
and all related to the YAML module, and it is now working correctly.

Yes, we fixed it by remote control. :wink:

-Dave

Dave A. wrote in post #1044017:

On Fri, Feb 3, 2012 at 18:02, Patrick B. [email protected]
wrote:

Just went back to it to get a copy of the errors, which were extensive,
and all related to the YAML module, and it is now working correctly.

Yes, we fixed it by remote control. :wink:

-Dave

I guessed as much!!! Seriously, wondering if something in the site code
got fixed, or altered.

Robert K. wrote in post #1044013:

On Fri, Feb 3, 2012 at 11:02 PM, Patrick B. [email protected]
wrote:

Some assistance please - I have a Ruby project, which I started in Ruby
1.8.6/7, some time ago. It involves several custom data classes, which
can have up to 500 multi-field items in “lists”. The size of the data
means that file storage is crucial, however, with the massive changes
made to yaml.rb in Ruby 1.9.n, I find that, although I can load existing
data from file, I can’t find a way to save it back to file. Anyone got
any suggestions where to start?

Did you consider using Marshal?

I’d prefer using a proper database but can’t get any of those to behave
properly either!!!

What does that mean?

Kind regards

robert

I did try using Marshal, however, while the examples show how to use
dump & load for in memory use, there was not enough information for me
to get it to write to a file! I don’t have enough Ruby experience yet to
work out how to do this sort of stuff - I’m a Pascal/Delphi man by
choice, and serializing objects is nearly all down to the coder.
As to the database comment, I have so far been unable to get mySQL
working with Ruby 1.9, and don’t (yet) know enough SQLite to make it’s
use practical. I have SQL Server too, but that has even poorer support
documentation, as far as Ruby is concerned. Most of my attempts have
failed at the connect stage.

On Sat, Feb 4, 2012 at 4:24 AM, Patrick B. [email protected]
wrote:

Did you consider using Marshal?

I’d prefer using a proper database but can’t get any of those to behave
properly either!!!

What does that mean?

I did try using Marshal, however, while the examples show how to use
dump & load for in memory use, there was not enough information for me
to get it to write to a file! I don’t have enough Ruby experience yet to
work out how to do this sort of stuff - I’m a Pascal/Delphi man by
choice, and serializing objects is nearly all down to the coder.

The docs at
http://www.ruby-doc.org/core-1.9.3/Marshal.html#method-c-dump
mention that you can pass an IO object.

file_name = “foo.bin”

store

File.open(file_name, ‘wb’) {|io| Marshal.dump(obj, io)}

load

obj = File.open(file_name, ‘rb’) {|io| Marshal.load(io)}

You should take care that “obj” is an object containing everything you
want to serialize.

As to the database comment, I have so far been unable to get mySQL
working with Ruby 1.9, and don’t (yet) know enough SQLite to make it’s
use practical. I have SQL Server too, but that has even poorer support
documentation, as far as Ruby is concerned. Most of my attempts have
failed at the connect stage.

I’d try Marshal first. Much simpler to use and quite fast.

Kind regards

robert

On Thu, Feb 9, 2012 at 2:23 AM, Patrick B. [email protected]
wrote:

file_name = “foo.bin”

As to the database comment, I have so far been unable to get mySQL
working with Ruby 1.9, and don’t (yet) know enough SQLite to make it’s
use practical. I have SQL Server too, but that has even poorer support
documentation, as far as Ruby is concerned. Most of my attempts have
failed at the connect stage.

I’d try Marshal first. Much simpler to use and quite fast.

Thanks for the assist Robert - marshal does indeed work - the key for me
was the IO reference, which is not well explained in the reference
material!
However, I have also discovered PStore, which is more convenient for me,
as it will keep several objects in the same file.

No, the main advantage of PStore over plain vanilla Marshal is
transactional behavior - not the fact that you can store Hashes. A
Hash can be easily stored with Marshal as well. As I said: “You
should take care that “obj” is an object containing everything you
want to serialize.” The only difference here is that with PStore you
can read and write individual key value pairs while with Marshal you
need to read and write the complete Hash. It is just an interface
thing though and PStore reads and writes the complete Hash internally
as well:

irb(main):010:0> f=‘x.pstore’
=> “x.pstore”
irb(main):013:0> ps = PStore.new f
=> #<PStore:0x202c55a4 @filename=“x.pstore”, @abort=false,
@ultra_safe=false, @thread_safe=false, @lock=#Mutex:0x202c54dc>

irb(main):014:0> ps.transaction {ps[‘a’]=Time.now}
=> 2012-02-09 14:12:17 +0100
irb(main):015:0> File.open(f,‘rb’) {|io| Marshal.load(io)}
=> {“a”=>2012-02-09 14:12:17 +0100}

irb(main):016:0> ps.transaction {ps[‘b’]=Time.now}
=> 2012-02-09 14:12:36 +0100
irb(main):017:0> File.open(f,‘rb’) {|io|Marshal.load(io)}
=> {“a”=>2012-02-09 14:12:17 +0100, “b”=>2012-02-09 14:12:36 +0100}

Kind regards

robert

Robert K. wrote in post #1044064:

On Sat, Feb 4, 2012 at 4:24 AM, Patrick B. [email protected]
wrote:

Did you consider using Marshal?

I’d prefer using a proper database but can’t get any of those to behave
properly either!!!

What does that mean?

I did try using Marshal, however, while the examples show how to use
dump & load for in memory use, there was not enough information for me
to get it to write to a file! I don’t have enough Ruby experience yet to
work out how to do this sort of stuff - I’m a Pascal/Delphi man by
choice, and serializing objects is nearly all down to the coder.

The docs at
http://www.ruby-doc.org/core-1.9.3/Marshal.html#method-c-dump
mention that you can pass an IO object.

file_name = “foo.bin”

store

File.open(file_name, ‘wb’) {|io| Marshal.dump(obj, io)}

load

obj = File.open(file_name, ‘rb’) {|io| Marshal.load(io)}

You should take care that “obj” is an object containing everything you
want to serialize.

As to the database comment, I have so far been unable to get mySQL
working with Ruby 1.9, and don’t (yet) know enough SQLite to make it’s
use practical. I have SQL Server too, but that has even poorer support
documentation, as far as Ruby is concerned. Most of my attempts have
failed at the connect stage.

I’d try Marshal first. Much simpler to use and quite fast.

Kind regards

robert
Thanks for the assist Robert - marshal does indeed work - the key for me
was the IO reference, which is not well explained in the reference
material!
However, I have also discovered PStore, which is more convenient for me,
as it will keep several objects in the same file.
Example
require ‘pstore’
require ‘vwobjects’

#m2 = Minions.new()
#m2 = File.open(‘minions.bin’, ‘rb’) {|io| Marshal.load(io)}

#m3 = Minions.new()
#m3 = File.open(‘specmins.bin’, ‘rb’) {|io| Marshal.load(io)}

store = PStore.new(“C:/temp/vwdata”)
store.transaction do
store[‘minions’] = m2
store[‘special’] = m3
end

#Now reload data
store.transaction do
puts “Roots: #{store.roots.join(’, ')}”
m2 = store[‘minions’]
puts"#{m2}"
m3 = store[‘special’]
puts"#{m3}"
end
m2 and m3 being custom objects which enclose an array, facilitating use
as a list.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs