Skip to content

Commit 5b33074

Browse files
committed
Add #rewind method to File operation
This is another method an IO-like object should provide. It resets the beginning state of the file.
1 parent 0de5236 commit 5b33074

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/net/sftp/operations/file.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ def readline(sep_or_limit=$/, limit=Float::INFINITY)
127127
return line
128128
end
129129

130+
# Resets position to beginning of file
131+
def rewind
132+
@pos = 0
133+
@real_pos = 0
134+
@real_eof = false
135+
@buffer = ""
136+
end
137+
130138
# Writes the given data to the stream, incrementing the file position and
131139
# returning the number of bytes written.
132140
def write(data)

test/test_file.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ def test_readline_should_raise_exception_on_EOF
133133
assert_raises(EOFError) { @file.readline }
134134
end
135135

136+
def test_rewind_should_reset_to_beginning_of_file
137+
@sftp.expects(:read!).times(2).returns("hello world", nil)
138+
@file.read
139+
assert @file.eof?
140+
@file.rewind
141+
assert !@file.eof?
142+
assert_equal 0, @file.pos
143+
end
144+
136145
def test_write_should_write_data_and_increment_pos_and_return_data_length
137146
@sftp.expects(:write!).with("handle", 0, "hello world")
138147
assert_equal 11, @file.write("hello world")

0 commit comments

Comments
 (0)