Class: RBI::Type::Proc

Inherits:
RBI::Type show all
Defined in:
lib/rbi/type.rb

Overview

A proc type like ‘T.proc.void`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RBI::Type

all, any, anything, attached_class, boolean, class_of, #eql?, generic, #hash, #nilable, nilable, #nilable?, #non_nilable, noreturn, parse_node, parse_string, proc, #rbs_string, self_type, shape, simple, t_class, #to_s, tuple, type_parameter, untyped, void

Constructor Details

#initializeProc

: -> void



678
679
680
681
682
683
# File 'lib/rbi/type.rb', line 678

def initialize
  super
  @proc_params = {} #: Hash[Symbol, Type]
  @proc_returns = Type.void #: Type
  @proc_bind = nil #: Type?
end

Instance Attribute Details

#proc_bindObject (readonly)

: Type?



675
676
677
# File 'lib/rbi/type.rb', line 675

def proc_bind
  @proc_bind
end

#proc_paramsObject (readonly)

: Hash[Symbol, Type]



669
670
671
# File 'lib/rbi/type.rb', line 669

def proc_params
  @proc_params
end

#proc_returnsObject (readonly)

: Type



672
673
674
# File 'lib/rbi/type.rb', line 672

def proc_returns
  @proc_returns
end

Instance Method Details

#==(other) ⇒ Object

: (BasicObject other) -> bool



687
688
689
690
691
692
693
694
# File 'lib/rbi/type.rb', line 687

def ==(other)
  return false unless Proc === other
  return false unless @proc_params == other.proc_params
  return false unless @proc_returns == other.proc_returns
  return false unless @proc_bind == other.proc_bind

  true
end

#bind(type) ⇒ Object

: (untyped type) -> self



715
716
717
718
# File 'lib/rbi/type.rb', line 715

def bind(type)
  @proc_bind = type
  self
end

#normalizeObject

: -> Type



747
748
749
# File 'lib/rbi/type.rb', line 747

def normalize
  self
end

#params(**params) ⇒ Object

: (**Type params) -> self



697
698
699
700
# File 'lib/rbi/type.rb', line 697

def params(**params)
  @proc_params = params
  self
end

#returns(type) ⇒ Object

: (untyped type) -> self



703
704
705
706
# File 'lib/rbi/type.rb', line 703

def returns(type)
  @proc_returns = type
  self
end

#simplifyObject

: -> Type



753
754
755
# File 'lib/rbi/type.rb', line 753

def simplify
  self
end

#to_rbiObject

: -> String



722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/rbi/type.rb', line 722

def to_rbi
  rbi = +"T.proc"

  if @proc_bind
    rbi << ".bind(#{@proc_bind})"
  end

  unless @proc_params.empty?
    rbi << ".params("
    rbi << @proc_params.map { |name, type| "#{name}: #{type.to_rbi}" }.join(", ")
    rbi << ")"
  end

  rbi << case @proc_returns
  when Void
    ".void"
  else
    ".returns(#{@proc_returns})"
  end

  rbi
end

#voidObject

: -> self



709
710
711
712
# File 'lib/rbi/type.rb', line 709

def void
  @proc_returns = RBI::Type.void
  self
end