Module: TTY::Conversion

Included in:
Table, Vector
Defined in:
lib/tty/support/conversion.rb

Overview

A mixin to allow instances conversion to different types

Instance Method Summary collapse

Instance Method Details

#convert_to_array(object, copy = false) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the object into an Array. If copy is set to true a copy of object will be made.

Parameters:

  • object (Object)
  • copy (Boolean) (defaults to: false)

Returns:

  • (Array)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tty/support/conversion.rb', line 17

def convert_to_array(object, copy=false)
  case object
  when Array
    copy ? object.dup : object
  when Hash
    Array(object)
  else
    begin
      converted = object.to_ary
    rescue Exception => e
      raise TTY::TypeError, "Cannot convert #{object.class} into an Array (#{e.message})"
    end
    converted
  end
end