Module: Rubinius::Type
- Defined in:
- lib/rubinius/kernel/common/type.rb
Class Method Summary collapse
-
.coerce_to(obj, cls, meth) ⇒ Object
Returns an object of given class.
- .coerce_to_comparison(a, b) ⇒ Object
- .coerce_to_symbol(obj) ⇒ Object
- .each_ancestor(mod) ⇒ Object
- .ivar_validate(name) ⇒ Object
-
.num2long(obj) ⇒ Object
Maps to rb_num2long in MRI.
-
.try_convert(obj, cls, meth) ⇒ Object
Same as coerce_to but returns nil if conversion fails.
Class Method Details
.coerce_to(obj, cls, meth) ⇒ Object
Returns an object of given class. If given object already is one, it is returned. Otherwise tries obj.meth and returns the result if it is of the right kind. TypeErrors are raised if the conversion method fails or the conversion result is wrong.
Uses Rubinius::Type.object_kind_of to bypass type check overrides.
Equivalent to MRI’s rb_convert_type().
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rubinius/kernel/common/type.rb', line 17 def self.coerce_to(obj, cls, meth) return obj if object_kind_of?(obj, cls) begin ret = obj.__send__(meth) rescue Exception => orig raise TypeError, "Coercion error: #{obj.inspect}.#{meth} => #{cls} failed", orig end return ret if object_kind_of?(ret, cls) msg = "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{object_class(ret)})" raise TypeError, msg end |
.coerce_to_comparison(a, b) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/rubinius/kernel/common/type.rb', line 63 def self.coerce_to_comparison(a, b) unless cmp = (a <=> b) raise ArgumentError, "comparison of #{a.inspect} with #{b.inspect} failed" end cmp end |
.coerce_to_symbol(obj) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/rubinius/kernel/common/type.rb', line 54 def self.coerce_to_symbol(obj) if object_kind_of?(obj, Fixnum) raise ArgumentError, "Fixnums (#{obj}) cannot be used as symbols" end obj = obj.to_str if obj.respond_to?(:to_str) coerce_to(obj, Symbol, :to_sym) end |
.each_ancestor(mod) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubinius/kernel/common/type.rb', line 79 def self.each_ancestor(mod) unless object_kind_of?(mod, Class) and singleton_class_object(mod) yield mod end sup = mod.direct_superclass() while sup if object_kind_of?(sup, IncludedModule) yield sup.module elsif object_kind_of?(sup, Class) yield sup unless singleton_class_object(sup) else yield sup end sup = sup.direct_superclass() end end |
.ivar_validate(name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rubinius/kernel/common/type.rb', line 97 def self.ivar_validate(name) # adapted from rb_to_id case name when String return name.to_sym if name[0] == ?@ when Symbol return name if name.is_ivar? when Fixnum raise ArgumentError, "#{name.inspect} is not a symbol" else name = Rubinius::Type.coerce_to(name, String, :to_str) return name.to_sym if name[0] == ?@ end raise NameError, "`#{name}' is not allowed as an instance variable name" end |
.num2long(obj) ⇒ Object
Maps to rb_num2long in MRI
71 72 73 74 75 76 77 |
# File 'lib/rubinius/kernel/common/type.rb', line 71 def self.num2long(obj) if obj == nil raise TypeError, "no implicit conversion from nil to integer" else Integer(obj) end end |
.try_convert(obj, cls, meth) ⇒ Object
Same as coerce_to but returns nil if conversion fails. Corresponds to MRI’s rb_check_convert_type()
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rubinius/kernel/common/type.rb', line 38 def self.try_convert(obj, cls, meth) return obj if object_kind_of?(obj, cls) return nil unless obj.respond_to?(meth) begin ret = obj.__send__(meth) rescue Exception return nil end return ret if ret.nil? || object_kind_of?(ret, cls) msg = "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{object_class(ret)})" raise TypeError, msg end |