class IO

Public Class Methods

binread(file, *arg) click to toggle source
# File lib/backports/1.9.1/io.rb, line 3
def binread(file, *arg)
  raise ArgumentError, "wrong number of arguments (#{1+arg.size} for 1..3)" unless arg.size < 3
  File.open(Backports.convert_to_path(file),"rb") do |f|
    f.read(*arg)
  end
end
binwrite(name, string, offset = nil, options = Backports::Undefined) click to toggle source

Standard in Ruby 1.9.3 See official documentation This method does support an options hash, see bugs.ruby-lang.org/issues/5782

# File lib/backports/1.9.3/io.rb, line 29
def binwrite(name, string, offset = nil, options = Backports::Undefined)
  Backports.write(true, name, string, offset, options)
end
open_with_options_hash(fd, mode = nil, options = Backports::Undefined) { |f| ... } click to toggle source
# File lib/backports/1.9.1/io.rb, line 17
def open_with_options_hash(fd, mode = nil, options = Backports::Undefined)
  mode = Backports.combine_mode_and_option(mode, options)
  # Can't backport autoclose, {internal|external|}encoding
  if block_given?
    open_without_options_hash(fd, mode){|f| yield f}
  else
    open_without_options_hash(fd, mode)
  end
end
try_convert(obj) click to toggle source
# File lib/backports/1.9.1/io.rb, line 10
def try_convert(obj)
  Backports.try_convert(obj, IO, :to_io)
end
write(name, string, offset = nil, options = Backports::Undefined) click to toggle source

Standard in Ruby 1.9.3 See official documentation

# File lib/backports/1.9.3/io.rb, line 23
def write(name, string, offset = nil, options = Backports::Undefined)
  Backports.write(false, name, string, offset, options)
end

Public Instance Methods

advise(advice, offset=0, len=0) click to toggle source

Standard in Ruby 1.9.3 See official documentation We're only for a platform not implementing advise, so we return nil.

# File lib/backports/1.9.3/io.rb, line 4
def advise(advice, offset=0, len=0)
  case advice
  when  :normal,
        :sequential,
        :random,
        :willneed,
        :dontneed,
        :noreuse
    return nil
  when Symbol
    raise NotImplementedError, "Unsupported advice #{advice}"
  else
    raise TypeError, "advice must be a Symbol"
  end
end
each_char() { |c| ... } click to toggle source
# File lib/backports/1.8.7/io.rb, line 9
def each_char
  return to_enum(:each_char) unless block_given?
  if $KCODE == "UTF-8"
    lookup = 7.downto(4)
    while c = read(1) do
      n = c[0]
      leftmost_zero_bit = lookup.find{|i| n[i].zero? }
      case leftmost_zero_bit
      when 7 # ASCII
        yield c
      when 6 # UTF 8 complementary characters
        next # Encoding error, ignore
      else
        more = read(6-leftmost_zero_bit)
        break unless more
        yield c+more
      end
    end
  else
    while s = read(1)
      yield s
    end
  end

  self
end