Should StringIO support to_str ?

  • Thread starter Hugh Sasse Staff Elec Eng
  • Start date
H

Hugh Sasse Staff Elec Eng

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
 
G

Gavin Sinclair

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 :)

But yeah, I support the argument.

Gavin
 
H

Hugh Sasse Staff Elec Eng


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"?
It's not an extension. It's a duck-extension :)

A platypus?
But yeah, I support the argument.

Gavin

Thank you,
Hugh
 
D

daz

Hugh said:
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 ;)


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)
 
G

Gavin Sinclair

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.

A platypus?

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


Gavin
 
H

Hal Fulton

Gavin said:
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
 
D

daz

Hal said:
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.
 
H

Hugh Sasse Staff Elec Eng

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 ;)

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 [x].pack("m*")
for some StringIO x.

irb(main):009:0> x << 'an example'
=> #<StringIO:0x259ddf8>
irb(main):010:0> [x].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
 
H

Hugh Sasse Staff Elec Eng

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 <[email protected]>
+
+ * ext/stringio.stringio.c: aliased string to to_str
+
Fri Apr 30 20:08:41 2004 WATANABE Hirofumi <[email protected]>

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

Yukihiro Matsumoto

Hi,

In message "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?

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.
 

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
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top