Should StringIO support to_str?

How is one supposed to get a string from a StringIO? If it is an
extension of string should it not support the new to_str method?

brains hgs 38 %> ./!$
./StringIOtest.rb
"#StringIO:0xf1310"
"#StringIO:0xf1310"
./StringIOtest.rb:12: undefined method `to_str’ for
#StringIO:0xf1310 (NoMethodError)
brains hgs 39 %> ruby --version
ruby 1.8.1 (2003-12-25) [sparc-solaris2.9]
brains hgs 40 %> cat ./StringIOtest.rb
#!/usr/local/bin/ruby

require ‘stringio’

string = "a string."
stream = StringIO.new(string)

stream << ’ More text’

p stream.inspect
p stream.to_s
p stream.to_str
brains hgs 41 %>

The result of p looks add

brains hgs 42 %> ruby -e “x = ‘y’; p x.inspect”
"“y”"
brains hgs 43 %>
is more what I was expecting – i.e the StringIO’s contents.

    Hugh

How is one supposed to get a string from a StringIO?

#string

If it is an extension of string should it not support the new to_str
method?

It’s not an extension. It’s a duck-extension :slight_smile:

But yeah, I support the argument.

Gavin

···

On Friday, April 30, 2004, 3:30:54 AM, Hugh wrote:

Hi,

···

In message “Should StringIO support to_str ?” on 04/04/30, Hugh Sasse Staff Elec Eng hgs@dmu.ac.uk writes:

How is one supposed to get a string from a StringIO? If it is an
extension of string should it not support the new to_str method?

I don’t think it should. StringIO is an IO (actually a duck quacks
like an IO) based on a string, but not a string itself.

						matz.

How is one supposed to get a string from a StringIO?

#string

That’s better, but I don’t see the part with which I initialized.
brains hgs 49 %> cat !$
cat StringIOtest.rb
#!/usr/local/bin/ruby

require ‘stringio’

string = “a string.”
stream = StringIO.new(string)

stream << ’ More text’

stream.rewind
p stream.inspect
p stream.to_s
p stream.string
p stream.string.class
brains hgs 50 %> !$
StringIOtest.rb
“#StringIO:0xf11d8
“#StringIO:0xf11d8
" More text"
String
brains hgs 51 %>

Why not “a string. More text”?

If it is an extension of string should it not support the new to_str
method?

It’s not an extension. It’s a duck-extension :slight_smile:

A platypus?

But yeah, I support the argument.

Gavin

    Thank you,
    Hugh
···

On Fri, 30 Apr 2004, Gavin Sinclair wrote:

On Friday, April 30, 2004, 3:30:54 AM, Hugh wrote:

Does this patch, as yet untested, provide a sufficient fix? I’m not
sure about the correct way to create ChangeLog entries, so hand
edited that with an approx time. (I believe I created this against
the 1.8 CVS, though I didn’t provide an exact release number to
the cvs update -d command done in the correct dir, so hope it
didn’t update from the head.)

    Hugh, meddling in tha affairs of wizards again!

— ruby/ext/stringio/stringio.c.orig 2004-04-30 14:50:00.564980000 +0100
+++ ruby/ext/stringio/stringio.c 2004-04-30 14:53:06.234702000 +0100
@@ -961,6 +961,7 @@
rb_define_method(StringIO, “reopen”, strio_reopen, -1);

 rb_define_method(StringIO, "string", strio_get_string, 0);
  • rb_define_method(StringIO, “to_str”, strio_get_string, 0);
    rb_define_method(StringIO, “string=”, strio_set_string, 1);
    rb_define_method(StringIO, “lineno”, strio_get_lineno, 0);
    rb_define_method(StringIO, “lineno=”, strio_set_lineno, 1);
    — ruby/ChangeLog.orig 2004-04-30 14:49:58.523946000 +0100
    +++ ruby/ChangeLog 2004-04-30 14:57:14.433008000 +0100
    @@ -1,3 +1,7 @@
···

+Fri Apr 30 15:00:00 2004 Hugh Sasse hgs@dmu.ac.uk
+

    • ext/stringio.stringio.c: aliased string to to_str

Fri Apr 30 20:08:41 2004 WATANABE Hirofumi eban@ruby-lang.org

* time.c (SIZEOF_TIME_T): support SIZEOF_TIME_T == SIZEOF_INT.

Hugh Sasse wrote:

Gavin Sinclair wrote:

#string

That’s better, but I don’t see the part with which I initialized.

The default open mode is ‘w+’, so append occurs at file pos = 0.
So, like an existing file, your init. string gets overwritten.

 try StringIO.new("text", 'a+')  # or 'a'

(Not defending, just observing :wink:

Aside:
StringIO in CVS ( ruby 1.9.0 (2004-04-27) [i586-bccwin32] )

···

#-----
require ‘stringio’

sio = StringIO.new
STDOUT.reopen(sio)
#-----

C:/TEMP/rb5226.TMP:5:in `reopen’: cannot convert StringIO into String (TypeError)

(I appreciate IO#reopen is undergoing changes; thanks)

Why not “a string. More text”?

Good question. I tend to use StringIO.new without an argument, but I
thought your use case was correct.

It’s not an extension. It’s a duck-extension :slight_smile:

A platypus?

Well, it may or may not be a platypus, but it’s certainly a monotreme.

Gavin

···

On Friday, April 30, 2004, 4:01:01 AM, Hugh wrote:

Gavin Sinclair wrote:

It’s not an extension. It’s a duck-extension :slight_smile:

A platypus?

Well, it may or may not be a platypus, but it’s certainly a monotreme.

That is only the second time I have heard that word, the first being
the song “Mammal” by They Might Be Giants.

/me sings under his breath, “…giraffe, etru, echidna, caribou…”

Cheers,
Hal

Hugh Sasse wrote:

Gavin Sinclair wrote:

#string

That’s better, but I don’t see the part with which I initialized.

The default open mode is ‘w+’, so append occurs at file pos = 0.
So, like an existing file, your init. string gets overwritten.

 try StringIO.new("text", 'a+')  # or 'a'

Oh, I see what is happening now, I think. Thank you.

(Not defending, just observing :wink:

No problem.

Aside:
StringIO in CVS ( ruby 1.9.0 (2004-04-27) [i586-bccwin32] )

#-----
require ‘stringio’

sio = StringIO.new
STDOUT.reopen(sio)
#-----

C:/TEMP/rb5226.TMP:5:in `reopen’: cannot convert StringIO into String (TypeError)

Is this because of the lack of to_str or because, to_s doesn’t give a
string, it gives a StringIO? I ran into this with .pack(“m*”)
for some StringIO x.

irb(main):009:0> x << ‘an example’
=> #StringIO:0x259ddf8
irb(main):010:0> .pack(‘m*’)
TypeError: cannot convert StringIO into String
from (irb):10:in `pack’
from (irb):10
irb(main):011:0>

So either this is a bug in pack because it doesn’t use metronomes –
I mean monotremes, or a bug in StringIO because to_s doesn’t do
“exactly what it says on the tin”. Unless my expectations are
wrong.

(I appreciate IO#reopen is undergoing changes; thanks)

    Thank you
    Hugh
···

On Fri, 30 Apr 2004, daz wrote:

Hal Fulton wrote:

Gavin Sinclair wrote:

Well, it may or may not be a platypus, but it’s certainly a monotreme.

That is only the second time I have heard that word, the first being
the song “Mammal” by They Might Be Giants.

On this musical theme:

p ‘monotreme’.sub(/(ono)(…)(e)/, ‘\3\2\1’)

daz

···


Could I just take this opportunity to say that the default open mode
for StringIO is ‘r+’ not ‘w+’ ?
‘w+’ would empty a string given to StringIO.new(string, ‘w+’).

Apologies for going [On Topic] in an [OT] thread.