Hello --

On Mon, 1 Jan 2001, Jean Michel wrote:

>  1- Derive a class from String that  has methods .read and .seek . I say
>  derive a  class because  I need  to add  an instance  variable 'current
>  pointer' and it  seems cleaner to have  this in a new  class. But since
>  instance  variables can  magically appear  maybe  I need  not derive  a
>  class. I can just do:
> 
>    class String
>      def as_file
>        @current_pointer=0
>      end
>      def read(n)
>        @current_pointer+=n
>        if @current_pointer>=length
>          @current_pointer=length-1
> 	 nil
>        else
>          @current_pointer+=n
> 	 self(@current_pointer-n..@current_pointer-1)
>        end
>      end
>      etc...

Just for fun, here's a very barebones version subclassed from String:

   class StringStream < String

     attr_accessor :pos

     def initialize(s)
       @pos = 0
       super(s)
     end

     def read(n)
       @pos += n
       self[@pos - n .. @pos - 1]
     end

     def seek(n)
       (@pos = n) < size
     end

     def tell
       @pos
     end

   end

In read(), I think it's good to let the pointer fall off the end,
because you get whatever's left of the string and then nil on the next
read.  

>  2- The key  point is to implement my functionality  .read_ID3V2tag in a
>  Module. Then  I can include  my module both in  class IO and  (the new)
>  class String, and voila! The job is done.

However, in getting String to read and seek, I think the main benefit
would be that you could then use that behavior transparently in your
ID3V2tag class (or module).  Having to go back and tell IO and String
individually how to handle the tag seems a little unwieldy.

For example:

   class Tag

     ...

     def get_tag(seekable)
       seekable.seek...
       seekable.read....
       @tag = ....
     end

     ...

   end


or something.  That way, your Tag code can handle any kind of stream
that seeks/reads/..., instead of your having to tell every such
streaming class about Tag.


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav