Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > read, write, seek method in a ring buffer class

Reply
Thread Tools

read, write, seek method in a ring buffer class

 
 
Martin Durai
Guest
Posts: n/a
 
      11-20-2007
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/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      11-20-2007
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

 
Reply With Quote
 
 
 
 
Martin Durai
Guest
Posts: n/a
 
      11-21-2007
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/.

 
Reply With Quote
 
Martin Durai
Guest
Posts: n/a
 
      11-21-2007
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/.

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      11-21-2007
On Nov 20, 8:13 pm, Martin Durai <(E-Mail Removed)> 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?
 
Reply With Quote
 
Martin Durai
Guest
Posts: n/a
 
      11-22-2007
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/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-22-2007
On Nov 22, 2007 9:59 AM, Martin Durai <(E-Mail Removed)> 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.

 
Reply With Quote
 
Martin Durai
Guest
Posts: n/a
 
      11-22-2007
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/.

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-22-2007
On Nov 22, 2007 11:41 AM, Martin Durai <(E-Mail Removed)> 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.

 
Reply With Quote
 
Martin Durai
Guest
Posts: n/a
 
      11-22-2007
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 <(E-Mail Removed)> 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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
RING RING RING Dashing through the wind, In a four dog dragged sleigh. Oh the street we go, laughing all the way Mathematician C++ 0 12-24-2006 02:26 PM
Digi-slave L-Ring Ultra II LED Ring Light Jim Hawkins Digital Photography 0 08-16-2005 12:12 PM
Ring ring....ring ring.... Slumpy Computer Support 1 07-06-2003 10:57 PM



Advertisments