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



396
397
398
399
400
401
# File 'lib/rbi/type.rb', line 396

def initialize
  super
  @proc_params = T.let({}, T::Hash[Symbol, Type])
  @proc_returns = T.let(Type.void, Type)
  @proc_bind = T.let(nil, T.nilable(Type))
end

Instance Attribute Details

#proc_bindObject (readonly)

: Type?



393
394
395
# File 'lib/rbi/type.rb', line 393

def proc_bind
  @proc_bind
end

#proc_paramsObject (readonly)

: Hash[Symbol, Type]



387
388
389
# File 'lib/rbi/type.rb', line 387

def proc_params
  @proc_params
end

#proc_returnsObject (readonly)

: Type



390
391
392
# File 'lib/rbi/type.rb', line 390

def proc_returns
  @proc_returns
end

Instance Method Details

#==(other) ⇒ Object

: (BasicObject other) -> bool



405
406
407
408
409
410
411
412
# File 'lib/rbi/type.rb', line 405

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



433
434
435
436
# File 'lib/rbi/type.rb', line 433

def bind(type)
  @proc_bind = type
  self
end

#params(**params) ⇒ Object

: (**Type params) -> self



415
416
417
418
# File 'lib/rbi/type.rb', line 415

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

#returns(type) ⇒ Object

: (untyped type) -> self



421
422
423
424
# File 'lib/rbi/type.rb', line 421

def returns(type)
  @proc_returns = type
  self
end

#to_rbiObject

: -> String



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/rbi/type.rb', line 440

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



427
428
429
430
# File 'lib/rbi/type.rb', line 427

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