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

Table =
{}
Symbol =
RubyClass.make(Symbol)
String =
RubyClass.make(String)
Integer =
RubyClass.make(Integer)
Float =
RubyClass.make(Float)
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!, get_instance_method_object, #has_role?, #is_super_of?, 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` returns `Void`.

Parameters:

  • clazz (Module|Object)

    a Ruby class or module.

Returns:



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

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

.make(clazz) ⇒ Object



302
303
304
305
306
# File 'lib/yadriggy/type.rb', line 302

def self.make(clazz)
  obj = RubyClass.new(clazz)
  Table[clazz] = obj
  obj
end

.set_alias(clazz, ruby_class) ⇒ Object



309
310
311
# File 'lib/yadriggy/type.rb', line 309

def self.set_alias(clazz, ruby_class)
  Table[clazz] = ruby_class
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`.



343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/yadriggy/type.rb', line 343

def <= (t)
  if t.is_super_of?(self)
    true
  else
    rc = RubyClass.role(t)
    if rc.nil?
      CommonSuperType.new(@ruby_class) <= t
    else
      rc.exact_type == @ruby_class || @ruby_class == ::NilClass
    end
  end
end

#==(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

#exact_typeObject



364
365
366
# File 'lib/yadriggy/type.rb', line 364

def exact_type
  @ruby_class
end

#get_method_object(method_name) ⇒ Object



357
358
359
360
361
# File 'lib/yadriggy/type.rb', line 357

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

#hashObject



334
335
336
# File 'lib/yadriggy/type.rb', line 334

def hash
  @ruby_class.hash
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