Class: Object

Inherits:
BasicObject
Defined in:
lib/union_type/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#__instance_of__?Object



26
# File 'lib/union_type/core_ext.rb', line 26

alias_method :__instance_of__?, :instance_of?

#__is_a__?Object



25
# File 'lib/union_type/core_ext.rb', line 25

alias_method :__is_a__?, :is_a?

#instance_of?(type) ⇒ Boolean

Returns true if this object’s exact class is one of the union’s members. Unlike #is_a?, subclass membership does not satisfy this check. Falls back to the standard instance_of? for plain Class arguments.

Examples:

42.instance_of?(Integer | String)  # => true
42.instance_of?(Numeric | String)  # => false (42 is not exactly Numeric)

Parameters:

Returns:

  • (Boolean)


53
54
55
# File 'lib/union_type/core_ext.rb', line 53

def instance_of?(type)
  type.__is_a__?(UnionType) ? type.include?(self.class) : __instance_of__?(type)
end

#is_a?(type) ⇒ Boolean Also known as: kind_of?

Returns true if this object is an instance of any class in the union. Falls back to the standard is_a? for plain Class or Module arguments.

Examples:

"hello".is_a?(String | Integer)  # => true
42.is_a?(String | Integer)       # => true
:sym.is_a?(String | Integer)     # => false

Parameters:

Returns:

  • (Boolean)


38
39
40
# File 'lib/union_type/core_ext.rb', line 38

def is_a?(type)
  type.__is_a__?(UnionType) ? type === self : __is_a__?(type)
end