471,310 Members | 1,068 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

read, write and seek method in ring buffer class

Could any body help me with creating a ring buffer class using a string. use memory circular buffer not an IO buffer. just read, write and seek method. Read method should take anumber and return the string. write method should take a string. seek should take a number and return nuthing. use three member variables a buffer itself as a string, read_position, write_position. use >>, << methods.

I have tried this code but i need full functionality. May be you can understand the functionality by the below code

<code>
class RingBuffer

# Instanciate a ring buffer of the given size.
# The buffer will contain at most +size+ elements

def initialize(size)

# initialize the max

@max = size

# initialize the buffer array

@buffer = Array.new(size)

# initialize the read position

@read_position = read_position

# initialize the write position

@write_position = write_position

end

#
# Method to clear the buffer
#

def clear

@buffer = []

end

#
# method call to read the contents in a buffer
#

def read(read_position)

# is read position is greater than the maximum

if @read_position > @max

# if so

raise "Illeagal Read Position, the value is greater than the maximum value of buffer"

# is read position is lesser than the minimm value

if @read_position < -1

# if so

raise " Illeagal Read Position, the value is less than minimum"

end

end

# read the buffer value

str = @buffer[read_position].to_s

# return the string

return str

end


#
# method call to write contents in a buffer
#

def write(value)


# shift the buffer

@buffer.shift

# append the value at the end

@buffer << value

end


#
# method call to convert buffer array
#

def to_a

# return the buffer element

return @buffer.dup

end

#
# method call to convert to string
#

def to_s

# initialize the str

str = ""

# each value in the buffer is converted to string

@buffer.each { |entry|
str << entry.to_s << "\n"

}

# return the string elements

return str

end

#
# method call to seek position
#

def seek




end

end
</code>

Thanks in advance
Nov 21 '07 #1
0 3718

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Ben Collingsworth | last post: by
7 posts views Thread by alexandre_irrthum | last post: by
6 posts views Thread by ericunfuk | last post: by
5 posts views Thread by dm3281 | last post: by
1 post views Thread by Steven D'Aprano | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.