Stringio as file

(J-Van) #1

StringIO can be used as a standin for a File object when testing, right?

require 'stringio'

def expects_file file
  file.open
  puts file.gets
end

s = StringIO.new "Hello World\nHow are you?"
expects_file s

What don't I understand here?

(J-Van) #2

Ah, nevermind. I forgot File.open is a class method.

···

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

StringIO can be used as a standin for a File object when testing, right?

require 'stringio'

def expects_file file
  file.open
  puts file.gets
end

s = StringIO.new "Hello World\nHow are you?"
expects_file s

What don't I understand here?

(J-Van) #3

This still confuses me though:

require 'stringio'

a = StringIO.new
a << "Hello\n"
a << "World"

puts a.gets # prints nil

···

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> StringIO can be used as a standin for a File object when testing, right?
>
> require 'stringio'
>
> def expects_file file
> file.open
> puts file.gets
> end
>
> s = StringIO.new "Hello World\nHow are you?"
> expects_file s
>
> What don't I understand here?

Ah, nevermind. I forgot File.open is a class method.

(Brian Mitchell) #4

> > StringIO can be used as a standin for a File object when testing, right?
> >
> > require 'stringio'
> >
> > def expects_file file
> > file.open
> > puts file.gets
> > end
> >
> > s = StringIO.new "Hello World\nHow are you?"
> > expects_file s
> >
> > What don't I understand here?
>
> Ah, nevermind. I forgot File.open is a class method.

This still confuses me though:

require 'stringio'

a = StringIO.new
a << "Hello\n"
a << "World"

try something along the lines of:
a.rewind

puts a.gets # prints nil

I would recommend you check the rdocs and see a.methods.sort

Brian.

···

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

(J-Van) #5

And further confusion:

require 'stringio'

a = StringIO.new
a << "Hello\n"
a << "World"
puts "a: #{ a.gets }"

b = StringIO.new "Hello\nWorld"
puts "b: #{ b.gets }"

Results in:
a:
b: Hello
         
So it appears that StringIO#<< isn't doing what I think it should be
doing. There's not much documentation on that method at ruby-doc.org
or in the Pickaxe2 book.

···

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > StringIO can be used as a standin for a File object when testing, right?
> >
> > require 'stringio'
> >
> > def expects_file file
> > file.open
> > puts file.gets
> > end
> >
> > s = StringIO.new "Hello World\nHow are you?"
> > expects_file s
> >
> > What don't I understand here?
>
> Ah, nevermind. I forgot File.open is a class method.

This still confuses me though:

require 'stringio'

a = StringIO.new
a << "Hello\n"
a << "World"

puts a.gets # prints nil

(Nobuyoshi Nakada) #6

Hi,

At Sat, 27 Aug 2005 06:43:29 +0900,
Joe Van Dyk wrote in [ruby-talk:153769]:

This still confuses me though:

require 'stringio'

a = StringIO.new
a << "Hello\n"
a << "World"

  a.rewind

···

puts a.gets # prints nil

--
Nobu Nakada

(J-Van) #7

Doh, that clears it up. Thanks!

···

On 8/26/05, Brian Mitchell <binary42@gmail.com> wrote:

On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > On 8/26/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > > StringIO can be used as a standin for a File object when testing, right?
> > >
> > > require 'stringio'
> > >
> > > def expects_file file
> > > file.open
> > > puts file.gets
> > > end
> > >
> > > s = StringIO.new "Hello World\nHow are you?"
> > > expects_file s
> > >
> > > What don't I understand here?
> >
> > Ah, nevermind. I forgot File.open is a class method.
>
> This still confuses me though:
>
> require 'stringio'
>
> a = StringIO.new
> a << "Hello\n"
> a << "World"
>

try something along the lines of:
a.rewind