Slide 71
Slide 71 text
def gets(separator="\n", length=nil)
if separator.nil?
read_all
elsif separator.is_a?(Integer)
read_length(separator)
elsif separator.length > 1
result = ''
len = 0
match_idx = 0
match_num = separator.length - 1
to_match = separator[match_idx].chr
if length
matcher = lambda {|idx, num| idx < num && len < length }
else
matcher = lambda {|idx, num| idx < num}
end
while matcher.call(match_idx, match_num) && char = getc
result << char
len += 1
if char == to_match
while match_idx < match_num do
match_idx += 1