Class: Yadriggy::InstanceType

Inherits:
Type
  • Object
show all
Defined in:
lib/yadriggy/type.rb

Overview

Type of a particular Ruby object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#!=, #copy, #eql?, error_found!, get_instance_method_object, #has_role?, #is_super_of?, role

Constructor Details

#initialize(obj) ⇒ InstanceType

Returns a new instance of InstanceType.



416
417
418
# File 'lib/yadriggy/type.rb', line 416

def initialize(obj)
  @object = obj
end

Instance Attribute Details

#objectObject (readonly)

Returns the Ruby object.

Returns:

  • (Object)

    the Ruby object.



414
415
416
# File 'lib/yadriggy/type.rb', line 414

def object
  @object
end

Instance Method Details

#<=(t) ⇒ Boolean

Check the subtype relation.

Parameters:

  • t (Type)

    the other type.

Returns:

  • (Boolean)

    true if ‘self` is equivalent to `t` or a subtype of `t`.



435
436
437
438
439
440
441
442
443
# File 'lib/yadriggy/type.rb', line 435

def <= (t)
  if t.is_super_of?(self)
    true
  else
    it = InstanceType.role(t)
    (!it.nil? && it.object == @object) ||
      RubyClass[exact_type] <= t
  end
end

#==(t) ⇒ Object



421
422
423
# File 'lib/yadriggy/type.rb', line 421

def == (t)
  InstanceType.role(t)&.object == @object
end

#exact_typeObject

Recall that ‘1.class` was `Fixnum` in Ruby earlier than 2.4.



447
448
449
# File 'lib/yadriggy/type.rb', line 447

def exact_type
  @object.is_a?(Integer) ? Integer : @object.class
end

#get_method_object(method_name) ⇒ Object



452
453
454
455
456
# File 'lib/yadriggy/type.rb', line 452

def get_method_object(method_name)
  @object.method(method_name)
rescue NameError
  Type.error_found!("no such method: #{@object.class}\##{method_name}")
end

#hashObject



426
427
428
# File 'lib/yadriggy/type.rb', line 426

def hash
  @object.hash
end

#nameString

Obtains the name of this type.

Returns:

  • (String)

    the type name.



465
466
467
# File 'lib/yadriggy/type.rb', line 465

def name
  @object.to_s
end

#supertypeRubyClass

Returns the RubyClass for this class.

Returns:



459
460
461
# File 'lib/yadriggy/type.rb', line 459

def supertype
  RubyClass[exact_type]
end