[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

read, write, seek method in a ring buffer class

dare ruby

11/20/2007 11:21:00 AM

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
--
Posted via http://www.ruby-....

11 Answers

James Gray

11/20/2007 1:17:00 PM

0

On Nov 20, 2007, at 5:21 AM, Martin Durai 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 Gray II

dare ruby

11/21/2007 3:14:00 AM

0

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

--
Posted via http://www.ruby-....

dare ruby

11/21/2007 6:55:00 AM

0

Martin Durai 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
--
Posted via http://www.ruby-....

Phrogz

11/21/2007 2:18:00 PM

0

On Nov 20, 8:13 pm, Martin Durai <mar...@angleritech.com> wrote:
> 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
>
> --
> Posted viahttp://www.ruby-....

It looks like you're off to a good start. What are your specific
problems?

dare ruby

11/22/2007 8:59:00 AM

0

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-....

Jesús Gabriel y Galán

11/22/2007 10:33:00 AM

0

On Nov 22, 2007 9:59 AM, Martin Durai <martin@angleritech.com> wrote:
> 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 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.

dare ruby

11/22/2007 10:41:00 AM

0

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

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

Thank you in advance
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

11/22/2007 11:20:00 AM

0

On Nov 22, 2007 11:41 AM, Martin Durai <martin@angleritech.com> wrote:
> Thank you Jes=FAs for spending your precious time but unfortunately i don=
t
> 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.

dare ruby

11/22/2007 12:16:00 PM

0

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 Durai <martin@angleritech.com> 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.

--
Posted via http://www.ruby-....

dare ruby

11/24/2007 3:02:00 AM

0

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 Durai 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 Durai <martin@angleritech.com> 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.

--
Posted via http://www.ruby-....