Skip to content

Commit

Permalink
IO.read reads on bytes instead of lines IO.read_lines reads on lines
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Dec 22, 2009
1 parent bcf7dae commit dfc96fb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
9 changes: 5 additions & 4 deletions lib/redisk/io.rb
Expand Up @@ -107,10 +107,11 @@ def self.pipe
# IO.read("testfile", 20) #=> "This is line one\nThi"
# IO.read("testfile", 20, 10) #=> "ne one\nThis is line "
def self.read(name, length = nil, offset = nil)
start_i = offset || 0
end_i = length ? start_i + (length - 1) : -1
values = redis.lrange(list_key(name), start_i, end_i)
values.join
io = new(name)
io.seek(offset) if offset
returned = io.read(length)
io.close
returned
end

# Works similarly to IO.read, but operates on lines instead of bytes
Expand Down
27 changes: 22 additions & 5 deletions spec/redisk_io_spec.rb
Expand Up @@ -82,12 +82,29 @@
Redisk::IO.read(@io_name).should == @file_as_array.join("")
end

it 'should return the first [length] bytes' do
Redisk::IO.read(@io_name, 2).should == @file_as_array[0][0..1]
end

it 'should return [length] contents starting at [offset]' do
Redisk::IO.read(@io_name, 2, 2).should == @file_as_array[0][2..3]
end

end


describe 'read_lines' do

it 'should return the entire contents without arguments' do
Redisk::IO.read_lines(@io_name).should == @file_as_array.join("")
end

it 'should return the first [length] contents' do
Redisk::IO.read(@io_name, 2).should == @file_as_array[0..1].join("")
Redisk::IO.read_lines(@io_name, 2).should == @file_as_array[0..1].join("")
end

it 'should return [length] contents starting at [offset]' do
Redisk::IO.read(@io_name, 2, 2).should == @file_as_array[2..3].join("")
Redisk::IO.read_lines(@io_name, 2, 2).should == @file_as_array[2..3].join("")
end

end
Expand Down Expand Up @@ -314,7 +331,7 @@

it 'should append arguments into a string and write to io' do
@io.print('My ', 'name ', 'is ', 'redisk')
line = Redisk::IO.read(@io_name, 1, 100)
line = Redisk::IO.read_lines(@io_name, 1, 100)
line.should == 'My name is redisk'
end

Expand All @@ -325,7 +342,7 @@
it 'should write the contents of $_ if no arguments are provided' do
@io.gets
@io.print
line = Redisk::IO.read(@io_name, 1, 100)
line = Redisk::IO.read_lines(@io_name, 1, 100)
line.should == @file_as_array[0]
end
end
Expand All @@ -334,7 +351,7 @@

it 'should run the arguments through sprintf and print to the io' do
@io.printf('My name is %s', 'redisk')
line = Redisk::IO.read(@io_name, 1, 100)
line = Redisk::IO.read_lines(@io_name, 1, 100)
line.should == 'My name is redisk'
end

Expand Down

0 comments on commit dfc96fb

Please sign in to comment.