| Class | CSV::StreamBuf |
| In: |
lib/csv.rb
|
| Parent: | Object |
Buffered stream.
EXAMPLE 1 — an IO.
class MyBuf < StreamBuf
# Do initialize myself before a super class. Super class might call my
# method 'read'. (Could be awful for C++ user. :-)
def initialize(s)
@s = s
super()
end
# define my own 'read' method.
# CAUTION: Returning nil means EnfOfStream.
def read(size)
@s.read(size)
end
# release buffers. in Ruby which has GC, you do not have to call this...
def terminate
@s = nil
super()
end
end
buf = MyBuf.new(STDIN)
my_str = ''
p buf[0, 0] # => '' (null string)
p buf[0] # => 97 (char code of 'a')
p buf[0, 1] # => 'a'
my_str = buf[0, 5]
p my_str # => 'abcde' (5 chars)
p buf[0, 6] # => "abcde\n" (6 chars)
p buf[0, 7] # => "abcde\n" (6 chars)
p buf.drop(3) # => 3 (dropped chars)
p buf.get(0, 2) # => 'de' (2 chars)
p buf.is_eos? # => false (is not EOS here)
p buf.drop(5) # => 3 (dropped chars)
p buf.is_eos? # => true (is EOS here)
p buf[0] # => nil (is EOS here)
EXAMPLE 2 — String.
This is a conceptual example. No pros with this.
class StrBuf < StreamBuf
def initialize(s)
@str = s
@idx = 0
super()
end
def read(size)
str = @str[@idx, size]
@idx += str.size
str
end
end
| BufSize | = | 1024 * 8 |
WARN: Do not instantiate this class directly. Define your own class which derives this class and define ‘read’ instance method.
get a char or a partial string from the stream. idx: index of a string to specify a start point of a string to get. unlike String instance, idx < 0 returns nil. n: size of a string to get. returns char at idx if n == nil. returns a partial string, from idx to (idx + n) if n != nil. at EOF, the string size could not equal to arg n.
ruby-doc.org is a service of James Britt and Neurogami, a Ruby application development company in Phoenix, AZ.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.