Hey, I'm learning Ruby and I've just about got it sorted. I have just one (or maybe more) question(s): What is marshal.dump and does it allow you to save data to a file outside of the Ruby program? If not, how do I create saved data (ie. character attributes inside a text-based game)? Thanks for answers!

Hey, I'm learning Ruby and I've just about got it sorted. I have just one (or maybe more) question(s): What is marshal.dump and does it allow you to save data to a file outside of the Ruby program? If not, how do I create saved data (ie. character attributes inside a text-based game)? Thanks for answers!

For some reason the rubycentral site seems to be down so the original article I'd link to isn't available, however it's in the google cache.

Hey, I'm learning Ruby and I've just about got it sorted. I have just one (or maybe more) question(s): What is marshal.dump and does it allow you to save data to a file outside of the Ruby program? If not, how do I create saved data (ie. character attributes inside a text-based game)? Thanks for answers!

You can read this small tutorial here -
http://sitekreator.com/satishtalim/object_serialization.html

Hope that helps.

Thanks, these sites really helped a lot and answered my questions, but the last one also brought up one little one: in the second example the programmer used

File.open('game', 'w+') do |f|

What does the string

'w+'

do and

|f|

do please? Thanks once again for any help.

Thanks, these sites really helped a lot and answered my questions, but the last one also brought up one little one: in the second example the programmer used

File.open('game', 'w+') do |f|

What does the string

'w+'

do and

|f|

do please? Thanks once again for any help.

"w+" is a mode string; it truncates the file (or creates a new one if it doesn't exist)

|f| is a block. Basically it means that in the following code f refers to the file.

A quick example of a simple block:

>> 5.times do |i|
?> puts i.to_s
>> end
0
1
2
3
4
=> 5

Thank you this really helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.