Class: YardTypes::LiteralType

Inherits:
Type
  • Object
show all
Defined in:
lib/yard_types/types.rb

Overview

A LiteralType constraint is specified by the name of one of YARD's supported "literals": true, false, nil, void, and self, and indicates that the object must be exactly one of those values.

However, void and self have no particular meaning: void is typically used solely to specify that a method returns no meaningful types; and self is used to specify that a method returns its receiver, generally to indicate that calls can be chained. All values type check as valid objects for void and self literals.

Instance Attribute Summary

Attributes inherited from Type

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Type

for, #initialize, #to_s

Constructor Details

This class inherits a constructor from YardTypes::Type

Class Method Details

.namesArray<String>


178
179
180
# File 'lib/yard_types/types.rb', line 178

def self.names
  @literal_names ||= %w(true false nil void self)
end

Instance Method Details

#check(obj) ⇒ Boolean

Returns true if the object is exactly true, false, or nil (depending on the value of name); for void and self types, this method always returns true.

Raises:

  • (NotImplementedError)

    if an unsupported literal name is to be tested against.


193
194
195
196
197
198
199
200
201
# File 'lib/yard_types/types.rb', line 193

def check(obj)
  case name
  when 'true'         then obj == true
  when 'false'        then obj == false
  when 'nil'          then obj == nil
  when 'self', 'void' then true
  else raise NotImplementedError, "Unsupported literal type: #{name.inspect}"
  end
end

#descriptionString


183
184
185
# File 'lib/yard_types/types.rb', line 183

def description
  name
end