Class: RBS::Substitution
- Inherits:
-
Object
- Object
- RBS::Substitution
- Defined in:
- lib/rbs/substitution.rb
Instance Attribute Summary collapse
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
Class Method Summary collapse
Instance Method Summary collapse
- #add(from:, to:) ⇒ Object
- #apply(ty) ⇒ Object
-
#initialize ⇒ Substitution
constructor
A new instance of Substitution.
- #without(*vars) ⇒ Object
Constructor Details
#initialize ⇒ Substitution
Returns a new instance of Substitution.
5 6 7 |
# File 'lib/rbs/substitution.rb', line 5 def initialize() @mapping = {} end |
Instance Attribute Details
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
3 4 5 |
# File 'lib/rbs/substitution.rb', line 3 def mapping @mapping end |
Class Method Details
.build(variables, types, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rbs/substitution.rb', line 13 def self.build(variables, types, &block) unless variables.size == types.size raise "Broken substitution: variables=#{variables}, types=#{types}" end mapping = variables.zip(types).to_h self.new.tap do |subst| mapping.each do |v, t| type = block_given? ? yield(t) : t subst.add(from: v, to: type) end end end |
Instance Method Details
#add(from:, to:) ⇒ Object
9 10 11 |
# File 'lib/rbs/substitution.rb', line 9 def add(from:, to:) mapping[from] = to end |
#apply(ty) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/rbs/substitution.rb', line 28 def apply(ty) case ty when Types::Variable mapping[ty.name] || ty else ty end end |
#without(*vars) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/rbs/substitution.rb', line 37 def without(*vars) self.class.new.tap do |subst| subst.mapping.merge!(mapping) vars.each do |var| subst.mapping.delete(var) end end end |