Class: IO

Inherits:
Object show all
Defined in:
lib/backports/1.9.3/io/advise.rb,
lib/backports/1.8.7/io/each_char.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.binread(file, length = nil, offset = 0) ⇒ Object


4
5
6
7
8
9
# File 'lib/backports/1.9.1/io/binread.rb', line 4

def IO.binread(file, length = nil, offset = 0)
  File.open(Backports.convert_path(file),"rb") do |f|
    f.seek(offset)
    f.read(length)
  end
end

.binwrite(name, string, offset = nil, options = Backports::Undefined) ⇒ Object


4
5
6
# File 'lib/backports/1.9.3/io/binwrite.rb', line 4

def IO.binwrite(name, string, offset = nil, options = Backports::Undefined)
  Backports.write(true, name, string, offset, options)
end

.try_convert(obj) ⇒ Object


4
5
6
# File 'lib/backports/1.9.1/io/try_convert.rb', line 4

def IO.try_convert(obj)
  Backports.try_convert(obj, IO, :to_io)
end

.write(name, string, offset = nil, options = Backports::Undefined) ⇒ Object


4
5
6
# File 'lib/backports/1.9.3/io/write.rb', line 4

def IO.write(name, string, offset = nil, options = Backports::Undefined)
  Backports.write(false, name, string, offset, options)
end

Instance Method Details

#advise(advice, offset = 0, len = 0) ⇒ Object

Raises:

  • (RangeError)

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/backports/1.9.3/io/advise.rb', line 5

def advise(advice, offset=0, len=0)
  raise RangeError if Backports.coerce_to_int(offset) >= 1<<31
  raise RangeError if Backports.coerce_to_int(len) >= 1<<31
  raise IOError if closed?
  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_charObject Also known as: chars


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/backports/1.8.7/io/each_char.rb', line 5

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