Module: TypeSafe
- Defined in:
- lib/typesafe.rb
Overview
TypeSafe module. This module will be included into Object to provide Object#must_be_kind_of.
Usage
If you need e.g. an argument be of a specific type, you can simply write
def myfunc(arg)
arg.must_be_kind_of(MyFancyClass)
arg.do_fancy_stuff
end
This would raise a TypeError in case you somehow passed in a type not matching your MyFancyClass, which would stop your app dead at a point you know. This is probably better than chasing the wrong parameter all the way down your application.
However, being dynamic, you might just need to apply a special treatment to your argument in case it’s not what you would expect:
def my2ndfunc(arg)
puts arg.must_be_kind_of(String) {|x| x.to_s}
end
In this second case, no exception is raised, but your block is executed. Most important, the return value of your block is returned as value from the method.
Instance Method Summary collapse
-
#must_be_kind_of(clss, &blk) ⇒ Object
(also: #must_be_a)
this class or object must be kind of a given class.
-
#must_be_nil_or_kind_of(clss, &blk) ⇒ Object
this class or object must be kind of a given class or nil.
Instance Method Details
#must_be_kind_of(clss, &blk) ⇒ Object Also known as: must_be_a
this class or object must be kind of a given class. Method returns self
unless it raises a TypeError.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/typesafe.rb', line 32 def must_be_kind_of(clss, &blk) if clss.kind_of?(Class) matched = if self.kind_of?(Class) # classes are expected to "descend" from comparator if self != clss self.superclass ? self.superclass.must_be_kind_of(clss) : self.kind_of?(clss) else true end else # objects are expected to implement comparator self.kind_of?(clss) end unless matched if blk return yield self.class else raise TypeError.new("#{self} must be of type #{clss}") end end elsif clss.kind_of?(Module) # check if the given module is included unless self.included_modules.include?(clss) if blk return yield self.class else raise TypeError.new("#{self} does not include module #{clss}") end end else raise "class to check #{self} for must be a Class or Module, but is #{clss}" end self end |
#must_be_nil_or_kind_of(clss, &blk) ⇒ Object
this class or object must be kind of a given class or nil. This method is probably a bad idea, so it will be removed.
71 72 73 |
# File 'lib/typesafe.rb', line 71 def must_be_nil_or_kind_of(clss, &blk) self.kind_of?(NilClass) ? true : must_be_kind_of(clss, &blk) end |