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.
Thanks in advance
On Nov 20, 2007, at 5:21 AM, Martin D. wrote:
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.
Show us what you have tried and tell us where you are stuck. We will
be happy to get you going again.
James Edward G. II
Iam new to this language, could u help me out to solve this problem
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
On Nov 20, 8:13 pm, Martin D. [email protected] wrote:
# initialize the write position
# is read position is greater than the maximum
if @read_position < -1
return @buffer.dup
end
end
–
Posted viahttp://www.ruby-forum.com/.
It looks like you’re off to a good start. What are your specific
problems?
Martin D. wrote:
Iam new to this language, could u help me out to solve this problem
Please james help me with code to solve this problem
Thank you in advance
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
[email protected]
# 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
[email protected]
# 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
Thank you Jesús for spending your precious time but unfortunately i dont
want to use IO buffers. i need to use buffer as a string. so could you
help me with code for my RingBuffer class displayed below
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
[email protected]
# 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
[email protected]
# 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
Thank you in advance
On Nov 22, 2007 9:59 AM, Martin D. [email protected] wrote:
seek should take number as a argument and return nothing. it should do
range checking. seek changes the read position.
I think you are doing too much work. I would wrap a StringIO which is
basically
what you want:
$ cat ringbuffer.rb && ruby ringbuffer.rb
require ‘stringio’
class RingBuffer
def initialize string = “”
@string_io = StringIO.new string
end
def read bytes = nil
@string_io.read bytes
end
def write string
cur_pos = @string_io.pos
@string_io.seek(0, IO::SEEK_END)
@string_io.write string
@string_io.seek(cur_pos, IO::SEEK_SET)
string.length
end
def seek bytes
@string_io.seek bytes
end
end
b = RingBuffer.new
b.write “abcde”
puts b.read
b.write “fghij”
puts b.read(2)
puts b.read(1)
b.write “12345”
puts b.read(5)
abcde
fg
h
ij123
Of course you might need to add some checks to fulfill your error
handling requirements but this might get you started.
Jesus.
Thank you once again jesus but could you do the same along with my
coding, without using any io buffers or string io buffers because iam
going to use this class in another application.
Jesús Gabriel y Galán wrote:
On Nov 22, 2007 11:41 AM, Martin D. [email protected] wrote:
Thank you Jes?r spending your precious time but unfortunately i dont
want to use IO buffers. i need to use buffer as a string.
StringIO is a String buffer. It just happens to have the same
methods as an IO:
read/write/seek and it keeps a position within the buffer.
Jesus.
On Nov 22, 2007 11:41 AM, Martin D. [email protected] wrote:
Thank you Jesús for spending your precious time but unfortunately i dont
want to use IO buffers. i need to use buffer as a string.
StringIO is a String buffer. It just happens to have the same
methods as an IO:
read/write/seek and it keeps a position within the buffer.
Jesus.
On Nov 22, 2007, at 6:15 AM, Martin D. wrote:
Thank you once again jesus but could you do the same along with my
coding, without using any io buffers or string io buffers because iam
going to use this class in another application.
Jesús’s code works just fine in other applications.
James Edward G. II
Thank you once again jesus but could you do the same along with my
coding, without using any io buffers or string io buffers because iam
going to use this class in another application.
Martin D. wrote:
Thank you once again jesus but could you do the same along with my
coding, without using any io buffers or string io buffers because iam
going to use this class in another application.
Jesús Gabriel y Galán wrote:
On Nov 22, 2007 11:41 AM, Martin D. [email protected] wrote:
Thank you Jes?r spending your precious time but unfortunately i dont
want to use IO buffers. i need to use buffer as a string.
StringIO is a String buffer. It just happens to have the same
methods as an IO:
read/write/seek and it keeps a position within the buffer.
Jesus.