Class: TypeStruct::Interface

Inherits:
Object
  • Object
show all
Includes:
Unionable
Defined in:
lib/type_struct/interface.rb,
lib/type_struct/ext.rb

Overview

IOLike = TypeStruct::Interface.new(

:read,
:write,
:close,
:closed?

) IOLike === StringIO.new #=> true IOLike === $stdin #=> true IOLike === 1 #=> false IOLike === “io” #=> false

case $stdin when IOLike

puts "this is a io like object!"

end

Instance Method Summary collapse

Methods included from Unionable

#|

Constructor Details

#initialize(*methods) ⇒ Interface

Returns a new instance of Interface.



21
22
23
# File 'lib/type_struct/interface.rb', line 21

def initialize(*methods)
  @methods = methods
end

Instance Method Details

#===(other) ⇒ Object



25
26
27
28
29
# File 'lib/type_struct/interface.rb', line 25

def ===(other)
  @methods.all? do |m|
    other.respond_to?(m)
  end
end

#inspectObject



29
30
31
# File 'lib/type_struct/ext.rb', line 29

def to_s
  "#<#{self.class.name.split('::').last}(#{@methods.map(&:inspect).join(',')})>"
end

#to_sObject



26
27
28
# File 'lib/type_struct/ext.rb', line 26

def to_s
  "#<#{self.class.name.split('::').last}(#{@methods.map(&:inspect).join(',')})>"
end