Why doesn't Ruby have a real StringBuffer or StringIO?

后端 未结 5 545
情话喂你
情话喂你 2020-12-09 07:25

I recently read a nice post on using StringIO in Ruby. What the author doesn\'t mention, though, is that StringIO is just an \"I.\" There\'s no \

5条回答
  •  情歌与酒
    2020-12-09 07:58

    Like other IO-type objects in Ruby, when you write to an IO, the character pointer advances.

    >> s = StringIO.new
    => #
    >> s << 'foo'
    => #
    >> s << 'bar'
    => #
    >> s.pos
    => 6
    >> s.rewind
    => 0
    >> s.read
    => "foobar"
    

提交回复
热议问题