Class: Yadriggy::RubyClass
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
-
.[](clazz) ⇒ RubyClass|Object
A RubyClass object for ‘clazz` if `clazz` is an instance of `Module`.
- .make(clazz) ⇒ Object
- .set_alias(clazz, ruby_class) ⇒ Object
Instance Method Summary collapse
-
#<=(t) ⇒ Boolean
Check the subtype relation.
-
#==(t) ⇒ Boolean
Checks the equality.
- #exact_type ⇒ Object
- #get_method_object(method_name) ⇒ Object
- #hash ⇒ Object
-
#initialize(clazz) ⇒ RubyClass
constructor
A new instance of RubyClass.
-
#name ⇒ String
Obtains the name of this type.
-
#supertype ⇒ CommonSuperType
The CommonSuperType for this class.
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.
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`.
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.
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.
329 330 331 |
# File 'lib/yadriggy/type.rb', line 329 def == (t) RubyClass.role(t)&.exact_type == @ruby_class end |
#exact_type ⇒ Object
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 |
#hash ⇒ Object
334 335 336 |
# File 'lib/yadriggy/type.rb', line 334 def hash @ruby_class.hash end |
#name ⇒ String
Obtains the name of this type.
375 376 377 |
# File 'lib/yadriggy/type.rb', line 375 def name @ruby_class.name end |
#supertype ⇒ CommonSuperType
Returns the CommonSuperType for this class.
369 370 371 |
# File 'lib/yadriggy/type.rb', line 369 def supertype CommonSuperType.new(@ruby_class) end |