Displaying 1 result from an estimated 1 matches for "count_lines2".
Did you mean:
count_lines
2012 Sep 23
1
ruby koans don't understand the principle sandwhich code
...rs in a smart pointer constructor is an attempt to deal with
# the problem of sandwich code for resource allocation.)
#
# Consider the following code:
#
def file_sandwich(file_name)
file = open(file_name)
yield(file)
ensure
file.close if file
end
# Now we write:
def count_lines2(file_name)
file_sandwich(file_name) do |file|
count = 0
while line = file.gets
count += 1
end
count
end
end
def test_counting_lines2
assert_equal 4, count_lines2("example_file.txt")
end
# -----------------------------------------------...