Beginner Questions

I’m hoping someone can give me a hand. I’ve having trouble recalling
information I’ve sent to a YAML file. I’m able to load the entire
document, but I’d like to just pull up one section of it (the $totaltime
global variable). My showDB pulls up fine, but the $totaltime always
resets to 0. I know I have $totaltime = 0 but I’m not sure how to make
$totaltime = $totaltime.to_yaml…if that make sense. If any
clarification is needed please let me know.

Here is a link to what I have:

http://codepad.org/mx5Qheyr

I’d really appreciate any guidance someone can offer.

Thanks!!
-T.J.

On Jan 19, 2012, at 09:13 , T.J. L. wrote:

Here is a link to what I have:

Ruby code - 118 lines - codepad

I’d really appreciate any guidance someone can offer.

Honestly, when you write code that looks like this:

def saveDB
File.open( $fn, ‘w’ ) {
# note the extra line with the massive amounts of trailing
whitespace…
|f|
f.write($col_array.to_yaml)
f.write($totaltime.to_yaml)
}
end

my brain stops reading almost immediately. Your indentation,
parenthesis, inconsistencies, and general styling all detract from the
readability of the code to the point where it’s hard to see the forest
through the trees.

Here is idiomatic ruby:

def save_db
File.open $filename, ‘w’ do |f|
f.write $dives.to_yaml
f.write $total_time.to_yaml # tho, this is entirely redundant and
could be recalculated
end
end

2 spaces per indent, snake case, no parens except as needed, real names.

Drop whatever language you’re coming from completely. Embrace ruby.

On Thu, Jan 19, 2012 at 10:46 PM, Ryan D. [email protected]
wrote:

def saveDB
File.open( $fn, ‘w’ ) {
# note the extra line with the massive amounts of trailing
whitespace…
|f|
f.write($col_array.to_yaml)
f.write($totaltime.to_yaml)
}
end

my brain stops reading almost immediately. Your indentation, parenthesis,
inconsistencies, and general styling all detract from the readability of the code
to the point where it’s hard to see the forest through the trees.

I found it difficult to digest as well.

Here is idiomatic ruby:

def save_db
File.open $filename, ‘w’ do |f|
f.write $dives.to_yaml
f.write $total_time.to_yaml # tho, this is entirely redundant and could be
recalculated
end
end

Another remark: $dives.to_yaml(f) is potentially more efficient since
output is directly written to the stream whereas the form with #write
first creates the complete string in memory before writing it.

Also I find there are too many global variables around. That limits
flexibility of the code.

Ah, and if total_time is the most important thing to read from the
stream it may make sense to reverse order of fields in the stream,
i.e. write total_time first.

Kind regards

robert

On Jan 19, 2012, at 14:22 , Robert K. wrote:

Another remark: $dives.to_yaml(f) is potentially more efficient since
output is directly written to the stream whereas the form with #write
first creates the complete string in memory before writing it.

Huh. I didn’t know you could do that. Normally I use YAML.dump and pass
the IO in.

I’m trying to teach myself, so I appreciate all the feedback, I still
can’t seem to solve the problem though. Like I said, I can get all of
the information to show but parsing through and getting particular
information leaves me stumped. How do I go about pulling just
“bottom_time” or “new_bottom” and displaying only that information.
Sorry if it’s not clear, I’m still trying to work my way through the
language.

On Thu, Jan 19, 2012 at 11:32 PM, Ryan D. [email protected]
wrote:

On Jan 19, 2012, at 14:22 , Robert K. wrote:

Another remark: $dives.to_yaml(f) is potentially more efficient since
output is directly written to the stream whereas the form with #write
first creates the complete string in memory before writing it.

Huh. I didn’t know you could do that. Normally I use YAML.dump and pass the IO
in.

Even better! That way you can easily exchange persistence with Marshal.

irb(main):004:0> data = {:now => Time.now}
=> {:now=>2012-01-20 11:53:17 +0100}
irb(main):005:0> f=“x”
=> “x”
irb(main):006:0> [Marshal, YAML].each do |perst|
irb(main):007:1* File.open(f,‘wb’){|io| perst.dump(data, io)}
irb(main):008:1> p File.open(f,‘rb’){|io| perst.load(io)}
irb(main):009:1> end
{:now=>2012-01-20 11:53:17 +0100}
{:now=>2012-01-20 11:53:17 +0100}
=> [Marshal, Psych]

:slight_smile:

Kind regards

robert

On Fri, Jan 20, 2012 at 3:59 PM, T.J. L. [email protected] wrote:

I’m trying to teach myself, so I appreciate all the feedback, I still
can’t seem to solve the problem though. Like I said, I can get all of
the information to show but parsing through and getting particular
information leaves me stumped. How do I go about pulling just
“bottom_time” or “new_bottom” and displaying only that information.
Sorry if it’s not clear, I’m still trying to work my way through the
language.

You need to extract the top level object and all nested objects that
contain the object you want to access. YAML and Marshal formats are
not intended to do queries on them. These are serialization formats
which let you write out a complete object graph. The easiest is to
just write out one object which references all the things you want
persisted.

Even if you would write items individually you need to remember the
order in which you have written manually and read the stream from the
beginning until you get to the point of interest - if you happen to
want to read the object you wrote last you need to read the whole
stream.

Kind regards

robert