stringio as file

J

Joe Van Dyk

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 =3D StringIO.new "Hello World\nHow are you?"
expects_file s

What don't I understand here?
 
J

Joe Van Dyk

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

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

Joe Van Dyk

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

This still confuses me though:

require 'stringio'

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

puts a.gets # prints nil
 
B

Brian Mitchell

=20
This still confuses me though:
=20
require 'stringio'
=20
a =3D StringIO.new
a << "Hello\n"
a << "World"
=20

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

Joe Van Dyk

=20
This still confuses me though:
=20
require 'stringio'
=20
a =3D StringIO.new
a << "Hello\n"
a << "World"
=20
puts a.gets # prints nil

And further confusion:

require 'stringio'

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

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

Results in:
a:
b: Hello
=20
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.
 
N

nobu.nokada

Hi,

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top