read, write, seek method in a ring buffer class

Discussion in 'Ruby' started by Martin Durai, Nov 20, 2007.

  1. Martin Durai

    Martin Durai Guest

    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-forum.com/.
    Martin Durai, Nov 20, 2007
    #1
    1. Advertising

  2. 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
    James Edward Gray II, Nov 20, 2007
    #2
    1. Advertising

  3. Martin Durai

    Martin Durai Guest

    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-forum.com/.
    Martin Durai, Nov 21, 2007
    #3
  4. Martin Durai

    Martin Durai Guest

    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-forum.com/.
    Martin Durai, Nov 21, 2007
    #4
  5. Martin Durai

    Phrogz Guest

    On Nov 20, 8:13 pm, Martin Durai <> 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-forum.com/.


    It looks like you're off to a good start. What are your specific
    problems?
    Phrogz, Nov 21, 2007
    #5
  6. Martin Durai

    Martin Durai Guest

    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/.
    Martin Durai, Nov 22, 2007
    #6
  7. On Nov 22, 2007 9:59 AM, Martin Durai <> 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.
    Jesús Gabriel y Galán, Nov 22, 2007
    #7
  8. Martin Durai

    Martin Durai Guest

    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-forum.com/.
    Martin Durai, Nov 22, 2007
    #8
  9. On Nov 22, 2007 11:41 AM, Martin Durai <> 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.
    Jesús Gabriel y Galán, Nov 22, 2007
    #9
  10. Martin Durai

    Martin Durai Guest

    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 <> 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-forum.com/.
    Martin Durai, Nov 22, 2007
    #10
  11. Martin Durai

    Martin Durai Guest

    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 <> 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-forum.com/.
    Martin Durai, Nov 24, 2007
    #11
  12. On Nov 22, 2007, at 6:15 AM, 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=FAs's code works just fine in other applications.

    James Edward Gray II=
    James Edward Gray II, Nov 26, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Collingsworth

    need ring buffer source code

    Ben Collingsworth, Sep 5, 2003, in forum: C Programming
    Replies:
    6
    Views:
    8,701
    Kevin Easton
    Sep 6, 2003
  2. Mathematician
    Replies:
    0
    Views:
    559
    Mathematician
    Dec 24, 2006
  3. Roedy Green

    OT ring ring

    Roedy Green, Aug 5, 2007, in forum: Java
    Replies:
    4
    Views:
    371
    Mike Schilling
    Aug 5, 2007
  4. David Merposa

    Ring Buffer Index

    David Merposa, Apr 8, 2009, in forum: C Programming
    Replies:
    6
    Views:
    776
    Flash Gordon
    Apr 10, 2009
  5. Replies:
    3
    Views:
    127
    Andreas Perstinger
    May 14, 2013
Loading...

Share This Page