I'm reading the Eloquent Ruby book (awesome book so far) and don't understand one section that talks about blocs, params, and scope. Here's the code...
class SomeApplication
# Rest of the class omitted...
def do_something
with_logging('load', nil) { @doc = Document.load( 'book' ) }
# Do something with the document...
with_logging('save', @doc) { |the_object| the_object.save }
end
def with_logging(description, the_object)
begin
@logger.debug( "Starting #{description}" )
yield( the_object )
@logger.debug( "Completed #{description}" )
rescue
@logger.error( "#{description} failed!!")
raise
end
end
end
The book says the code is more complex than it needs to be because @doc is automatically visible inside the code block. So there's no need to pass it down as an argument.
I don't understand which param he's talking about, the @doc or |the_object|. What would this code look like after changing it to remove the unnecessary complexity?
Or does it mean that the @doc that was created in with_logging('load', nil) is what is still visible? Even if that's the case I'm not sure how the with_logging method at the bottom would access it.