Class: RBS::Types::Variable

Inherits:
Object
  • Object
show all
Includes:
EmptyEachType
Defined in:
lib/rbs/types.rb

Constant Summary collapse

@@count =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EmptyEachType

#each_type

Constructor Details

#initialize(name:, location:) ⇒ Variable

Returns a new instance of Variable.



93
94
95
96
# File 'lib/rbs/types.rb', line 93

def initialize(name:, location:)
  @name = name
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



91
92
93
# File 'lib/rbs/types.rb', line 91

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



90
91
92
# File 'lib/rbs/types.rb', line 90

def name
  @name
end

Class Method Details

.build(v) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/rbs/types.rb', line 122

def self.build(v)
  case v
  when Symbol
    new(name: v, location: nil)
  when Array
    v.map {|x| new(name: x, location: nil) }
  else
    raise
  end
end

.fresh(v = :T) ⇒ Object



134
135
136
137
# File 'lib/rbs/types.rb', line 134

def self.fresh(v = :T)
  @@count = @@count + 1
  new(name: :"#{v}@#{@@count}", location: nil)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



98
99
100
# File 'lib/rbs/types.rb', line 98

def ==(other)
  other.is_a?(Variable) && other.name == name
end

#free_variables(set = Set.new) ⇒ Object



108
109
110
111
112
# File 'lib/rbs/types.rb', line 108

def free_variables(set = Set.new)
  set.tap do
    set << name
  end
end

#hashObject



104
105
106
# File 'lib/rbs/types.rb', line 104

def hash
  self.class.hash ^ name.hash
end

#sub(s) ⇒ Object



118
119
120
# File 'lib/rbs/types.rb', line 118

def sub(s)
  s.apply(self)
end

#to_json(*a) ⇒ Object



114
115
116
# File 'lib/rbs/types.rb', line 114

def to_json(*a)
  { class: :variable, name: name, location: location }.to_json(*a)
end

#to_s(level = 0) ⇒ Object



139
140
141
# File 'lib/rbs/types.rb', line 139

def to_s(level = 0)
  name.to_s
end