PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > read, write, seek method in a ring buffer class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
read, write, seek method in a ring buffer class

Réponse
 
LinkBack Outils de la discussion
Vieux 20/11/2007, 11h21   #1
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut read, write, seek method in a ring buffer class

Could any body 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/.

  Réponse avec citation
Vieux 20/11/2007, 13h16   #2
James Edward Gray II
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

On Nov 20, 2007, at 5:21 AM, Martin Durai wrote:

> Could any body 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

  Réponse avec citation
Vieux 21/11/2007, 03h13   #3
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

Iam new to this language, could u 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/.

  Réponse avec citation
Vieux 21/11/2007, 06h55   #4
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

Martin Durai wrote:
Iam new to this language, could u me out to solve this problem

Please james me with code to solve this problem


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

  Réponse avec citation
Vieux 21/11/2007, 14h18   #5
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

On Nov 20, 8:13 pm, Martin Durai <mar...@angleritech.com> wrote:
> Iam new to this language, could u 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?
  Réponse avec citation
Vieux 22/11/2007, 08h59   #6
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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

  Réponse avec citation
Vieux 22/11/2007, 10h33   #7
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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.

  Réponse avec citation
Vieux 22/11/2007, 10h41   #8
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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

  Réponse avec citation
Vieux 22/11/2007, 11h19   #9
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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.

  Réponse avec citation
Vieux 22/11/2007, 12h15   #10
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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-forum.com/.

  Réponse avec citation
Vieux 24/11/2007, 03h01   #11
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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-forum.com/.

  Réponse avec citation
Vieux 26/11/2007, 18h54   #12
James Edward Gray II
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: read, write, seek method in a ring buffer class

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=

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,28493 seconds with 20 queries