What do you think of giving the possibility of using blocks in the function gets?
Simple example:
def gets(sep=$/, limit=nil)
if block_given?
loop do
yield(gets_p(sep,limit))
end
else
gets_p(sep, limit)
end
end
private
def gets_p(sep, limit)
sep = "\n\n" if sep == ''
# This allows the method signature to be (sep) or (limit)
(limit = sep; sep="\n") if sep.is_a? Integer
bytes = []
loop do
current_byte = getbyte
bytes << current_byte unless current_byte.nil?
break if (bytes.last(sep.bytes.to_a.size) == sep.bytes.to_a) || ((bytes.size == limit) if limit)
end
bytes.map { |e| e.chr }.join
end
Hello guys,
What do you think of giving the possibility of using blocks in the function
gets
?Simple example:
Use:
or