Class: RBS::Types::Union

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(types:, location:) ⇒ Union

Returns a new instance of Union.



458
459
460
461
# File 'lib/rbs/types.rb', line 458

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

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



456
457
458
# File 'lib/rbs/types.rb', line 456

def location
  @location
end

#typesObject (readonly)

Returns the value of attribute types.



455
456
457
# File 'lib/rbs/types.rb', line 455

def types
  @types
end

Instance Method Details

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



463
464
465
# File 'lib/rbs/types.rb', line 463

def ==(other)
  other.is_a?(Union) && other.types == types
end

#each_type(&block) ⇒ Object



498
499
500
501
502
503
504
# File 'lib/rbs/types.rb', line 498

def each_type(&block)
  if block_given?
    types.each(&block)
  else
    enum_for :each_type
  end
end

#free_variables(set = Set.new) ⇒ Object



473
474
475
476
477
478
479
# File 'lib/rbs/types.rb', line 473

def free_variables(set = Set.new)
  set.tap do
    types.each do |type|
      type.free_variables set
    end
  end
end

#hashObject



469
470
471
# File 'lib/rbs/types.rb', line 469

def hash
  self.class.hash ^ types.hash
end

#map_type(&block) ⇒ Object



506
507
508
509
510
511
512
# File 'lib/rbs/types.rb', line 506

def map_type(&block)
  if block_given?
    Union.new(types: types.map(&block), location: location)
  else
    enum_for :map_type
  end
end

#sub(s) ⇒ Object



485
486
487
488
# File 'lib/rbs/types.rb', line 485

def sub(s)
  self.class.new(types: types.map {|ty| ty.sub(s) },
                 location: location)
end

#to_json(*a) ⇒ Object



481
482
483
# File 'lib/rbs/types.rb', line 481

def to_json(*a)
  { class: :union, types: types, location: location }.to_json(*a)
end

#to_s(level = 0) ⇒ Object



490
491
492
493
494
495
496
# File 'lib/rbs/types.rb', line 490

def to_s(level = 0)
  if level > 0
    "(#{types.join(" | ")})"
  else
    types.join(" | ")
  end
end