Class: Yadriggy::RubyClass

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

Overview

Type of immediate instances of a Ruby class. The instances of its subclass are excluded. A class type including its subclasses is represented by CommonSuperType.

Constant Summary collapse

Symbol =
RubyClass.make(Symbol)
String =
RubyClass.make(String)
Integer =
RubyClass.make(Integer)
Float =
RubyClass.make(Float)
Rational =
RubyClass.make(Rational)
Complex =
RubyClass.make(Complex)
Range =
RubyClass.make(Range)
Hash =
RubyClass.make(Hash)
Array =
RubyClass.make(Array)
Proc =
RubyClass.make(Proc)
Exception =
RubyClass.make(Exception)
TrueClass =
RubyClass.make(TrueClass)
FalseClass =
RubyClass.make(FalseClass)
NilClass =

The type for nil. Although ::NilClass is not a subtype of other classes, RubyClass::NilClass is a subtype of String, etc.

RubyClass.make(NilClass)
Boolean =

An instance of UnionType. It represents either TrueClass or FalseClass.

UnionType.new([RubyClass::TrueClass,
RubyClass::FalseClass])
Numeric =

An instance of CommonSuperType. It represents ::Numeric class.

CommonSuperType.new(Numeric)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Type

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

Constructor Details

#initialize(clazz) ⇒ RubyClass

Returns a new instance of RubyClass.

Parameters:

  • clazz (Module)

    the Ruby class or module.



322
323
324
# File 'lib/yadriggy/type.rb', line 322

def initialize(clazz)
  @ruby_class = clazz
end

Class Method Details

.[](clazz) ⇒ RubyClass|Object

Returns a Yadriggy::RubyClass object for clazz if clazz is an instance of Module. Otherwise, clazz is returned. For example, RubyClass[Void] returns Void.

Parameters:

  • clazz (Module|Object)

    a Ruby class or module.

Returns:

  • (RubyClass|Object)

    a Yadriggy::RubyClass object for clazz if clazz is an instance of Module. Otherwise, clazz is returned. For example, RubyClass[Void] returns Void.



317
318
319
# File 'lib/yadriggy/type.rb', line 317

def self.[](clazz)
  Table[clazz] || (clazz.is_a?(::Module) ? RubyClass.new(clazz) : clazz)
end

Instance Method Details

#==(t) ⇒ Boolean

Checks the equality.

Parameters:

  • t (Type|Module)

    the other object.

Returns:

  • (Boolean)

    true if self and t represent the same Ruby class.



329
330
331
# File 'lib/yadriggy/type.rb', line 329

def == (t)
  RubyClass.role(t)&.exact_type == @ruby_class
end

#nameString

Obtains the name of this type.

Returns:



375
376
377
# File 'lib/yadriggy/type.rb', line 375

def name
  @ruby_class.name
end

#supertypeCommonSuperType

Returns the CommonSuperType for this class.

Returns:



369
370
371
# File 'lib/yadriggy/type.rb', line 369

def supertype
  CommonSuperType.new(@ruby_class)
end