Pretending a String is a File : StringIO « File Directory « Ruby






Pretending a String is a File


require 'stringio'
s = StringIO.new %{I am the very model of a modern major general.
I've information vegetable, animal, and mineral.}


puts s.pos                                 # => 0
s.each_line { |x| puts x }
puts s.eof?                                # => true
puts s.pos                                 # => 95
puts s.rewind
puts s.pos                                 # => 0
puts s.grep /general/

 








Related examples in the same category

1.Performing Random Access on Read-Once Input Streams
2.Write to a string
3.YAML.dump
4.Find the fifth byte
5.push data to string
6.Redirecting Standard Input or Output