summaryrefslogtreecommitdiff
path: root/lib/method_source.rb
diff options
context:
space:
mode:
authorJohn Mair <jrmair@gmail.com>2010-12-19 21:49:26 +1300
committerJohn Mair <jrmair@gmail.com>2010-12-19 21:49:26 +1300
commit8f6b36637263a79ae4ac96ec734b1851614a4e19 (patch)
treed7240270e2653731d3a109aefdbf5c5dcc37a015 /lib/method_source.rb
parentac16884afaead1aa91bd747f0fee5aff2bd3abeb (diff)
downloadmethod_source-8f6b36637263a79ae4ac96ec734b1851614a4e19.tar.gz
refactor to use File block instead of manually closing
Diffstat (limited to 'lib/method_source.rb')
-rw-r--r--lib/method_source.rb49
1 files changed, 23 insertions, 26 deletions
diff --git a/lib/method_source.rb b/lib/method_source.rb
index 93a1109..d6a6e29 100644
--- a/lib/method_source.rb
+++ b/lib/method_source.rb
@@ -27,19 +27,17 @@ module MethodSource
return nil if !source_location.is_a?(Array)
file_name, line = source_location
- file = File.open(file_name)
- (line - 1).times { file.readline }
+ File.open(file_name) do |file|
+ (line - 1).times { file.readline }
- code = ""
- loop do
- val = file.readline
- code << val
-
- return code if valid_expression?(code)
- end
-
- ensure
- file.close if file
+ code = ""
+ loop do
+ val = file.readline
+ code << val
+
+ return code if valid_expression?(code)
+ end
+ end
end
# Helper method responsible for opening source file and buffering up
@@ -51,22 +49,21 @@ module MethodSource
return nil if !source_location.is_a?(Array)
file_name, line = source_location
- file = File.open(file_name)
- buffer = ""
- (line - 1).times do
- line = file.readline
- # Add any line that is a valid ruby comment,
- # but clear as soon as we hit a non comment line.
- if (line =~ /^\s*#/) || (line =~ /^\s*$/)
- buffer << line.lstrip
- else
- buffer.clear
+ File.open(file_name) do |file|
+ buffer = ""
+ (line - 1).times do
+ line = file.readline
+ # Add any line that is a valid ruby comment,
+ # but clear as soon as we hit a non comment line.
+ if (line =~ /^\s*#/) || (line =~ /^\s*$/)
+ buffer << line.lstrip
+ else
+ buffer.clear
+ end
end
+
+ buffer
end
-
- buffer
- ensure
- file.close if file
end
# This module is to be included by `Method` and `UnboundMethod` and