From: Martin Durai Date: 2007-11-22T17:59:19+09:00 Subject: Re: read, write, seek method in a ring buffer class Actually i need to create a ring buffer class, with read, write, seek methods. Read should take number as an argument(number of bytes to read) and should return a string. when you read, you need to read either the request number of bytes or the availlable number of bytes, whichever is smaller. when you read you advance forward the read_position. similarly write method should take string as a argument. now advance forward the write position. seek should take number as a argument and return nothing. it should do range checking. seek changes the read position. I have attached my code with this mail please make necessary changes in it and also help me to code seek method. Thanks in advance class RingBuffer # Instanciate a ring buffer of the given size. # The buffer will contain at most +size+ elements def initialize(size) # initialize the maximum @max = size #Initialize the buffer @buffer = Array.new end # # method call to read contents from the buffer # def read(number_of_bytes) #------------------------------------ # initialize the buffer size buffer_size=@buffer.length # is number of bytes greater than buffer size if number_of_bytes < buffer_size # if so # initialize the new buffer array @newBuffer = Array.new # initialize new buffer @newBuffer = @buffer[0..number_of_bytes] # return all elements in buffer as string @newBuffer.each() { |block| return block.to_s} #if not else # initialize the newbuffer array @newBuffer = Array.new # initialize the now buffer @newBuffer = @buffer[0..buffer_size] # return all the elements in buffer as string @newBuffer.each() { |block| puts block.to_s} end end # # method call to write contents in the buffer # def write(string_value) #----------------------------- #initialize the buffer size buffer_size=@buffer.length # is buffer size is greater than maximum if buffer_size > @max # if so # remove the first element in the buffer @buffer.shift # if not else # write a element to the buffer @buffer << string_value end end def seek end end -- Posted via http://www.ruby-forum.com/.