Module: Matrix::CoercionHelper

Included in:
Matrix, Scalar, Vector
Defined in:
opal/stdlib/matrix.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.coerce_to(obj, cls, meth) ⇒ Object

Helper method to coerce a value into a specific class. Raises a TypeError if the coercion fails or the returned value is not of the right class. (from Rubinius)

Raises:

  • (TypeError)


1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
# File 'opal/stdlib/matrix.rb', line 1549

def self.coerce_to(obj, cls, meth) # :nodoc:
  return obj if obj.kind_of?(cls)
  raise TypeError, "Expected a #{cls} but got a #{obj.class}" unless obj.respond_to? meth
  begin
    ret = obj.__send__(meth)
  rescue Exception => e
    raise TypeError, "Coercion error: #{obj.inspect}.#{meth} => #{cls} failed:\n" \
                     "(#{e.message})"
  end
  raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{ret.class})" unless ret.kind_of? cls
  ret
end

.coerce_to_int(obj) ⇒ Object



1562
1563
1564
# File 'opal/stdlib/matrix.rb', line 1562

def self.coerce_to_int(obj)
  coerce_to(obj, Integer, :to_int)
end

.coerce_to_matrix(obj) ⇒ Object



1566
1567
1568
# File 'opal/stdlib/matrix.rb', line 1566

def self.coerce_to_matrix(obj)
  coerce_to(obj, Matrix, :to_matrix)
end