Class: XRB::IOBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/xrb/buffer.rb

Overview

Represents an IO buffer with a given path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, path: io.inspect) ⇒ IOBuffer

Create a new IO buffer from an IO object.



104
105
106
107
108
# File 'lib/xrb/buffer.rb', line 104

def initialize(io, path: io.inspect)
  @io = io
  @path = path
  @cache = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



120
121
122
# File 'lib/xrb/buffer.rb', line 120

def path
  @path
end

#the path name of the buffer.(pathnameofthebuffer.) ⇒ Object (readonly)



120
# File 'lib/xrb/buffer.rb', line 120

attr :path

Instance Method Details

#encodingObject



123
124
125
# File 'lib/xrb/buffer.rb', line 123

def encoding
  read.encoding
end

#freezeObject

Freeze the buffer, caching the contents of the IO object.



111
112
113
114
115
116
117
# File 'lib/xrb/buffer.rb', line 111

def freeze
  return self if frozen?
  
  read
  
  super
end

#readObject

Read the contents of the IO object into the buffer.



128
129
130
131
132
133
134
135
# File 'lib/xrb/buffer.rb', line 128

def read
  unless @cache
    @cache = @io.read.freeze
    @io.close
  end
  
  return @cache
end

#to_bufferObject



138
139
140
# File 'lib/xrb/buffer.rb', line 138

def to_buffer
  Buffer.new(self.read, path: @path)
end