Method: PDF::Reader::ObjectHash#deref_number

Defined in:
lib/pdf/reader/object_hash.rb

#deref_number(key) ⇒ Object

If key is a PDF::Reader::Reference object, lookup the corresponding object in the PDF and return it. Otherwise return key untouched.

Guaranteed to only return a Numeric or nil. If the dereference results in any other type then a MalformedPDFError exception will raise. Useful when expecting an Array and no other type will do.

Some effort to cast to a number is made when the reference points to a non-number. : (untyped) -> Numeric?



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/pdf/reader/object_hash.rb', line 246

def deref_number(key)
  obj = deref(key)

  return obj if obj.nil?

  if !obj.is_a?(Numeric)
    if obj.respond_to?(:to_f)
      obj = obj.to_f
    elsif obj.respond_to?(:to_i)
      obj.to_i
    else
      raise MalformedPDFError, "expected object to be a number"
    end
  end

  obj
end