Module: Opal

Defined in:
opal/opal/corelib/helpers.rb

Class Method Summary collapse

Class Method Details

.coerce_to(object, type, method) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'opal/opal/corelib/helpers.rb', line 2

def self.coerce_to(object, type, method)
  return object if type === object

  unless object.respond_to? method
    raise TypeError, "no implicit conversion of #{object.class} into #{type}"
  end

  object.__send__ method
end

.coerce_to!(object, type, method) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'opal/opal/corelib/helpers.rb', line 12

def self.coerce_to!(object, type, method)
  coerced = coerce_to(object, type, method)

  unless type === coerced
    raise TypeError, "can't convert #{object.class} into #{type} (#{object.class}##{method} gives #{coerced.class}"
  end

  coerced
end

.compare(a, b) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'opal/opal/corelib/helpers.rb', line 30

def self.compare(a, b)
  compare = a <=> b

  if `compare === nil`
    raise ArgumentError, "comparison of #{a.class.name} with #{b.class.name} failed"
  end

  compare
end

.destructure(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'opal/opal/corelib/helpers.rb', line 54

def self.destructure(args)
  %x{
    if (args.length == 1) {
      return args[0];
    }
    else if (args._isArray) {
      return args;
    }
    else {
      return $slice.call(args);
    }
  }
end

.fits_array!(value) ⇒ Object



47
48
49
50
51
52
# File 'opal/opal/corelib/helpers.rb', line 47

def self.fits_array!(value)
  # this is the computed ARY_MAX_SIZE for 32 bit
  if `value >= 536870910`
    raise ArgumentError, "argument too big"
  end
end

.fits_fixnum!(value) ⇒ Object



40
41
42
43
44
45
# File 'opal/opal/corelib/helpers.rb', line 40

def self.fits_fixnum!(value)
  # since we have Fixnum#size as 32 bit, this is based on the int limits
  if `value > 2147483648`
    raise RangeError, "bignum too big to convert into `long'"
  end
end

.try_convert(object, type, method) ⇒ Object



22
23
24
25
26
27
28
# File 'opal/opal/corelib/helpers.rb', line 22

def self.try_convert(object, type, method)
  return object if type === object

  if object.respond_to? method
    object.__send__ method
  end
end