-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
Consider the obj_size1 method in the previous section, which worked with most objects that respond to size. The static typing systems have limited support for duck-typed methods like this. However, for both Steep and Sorbet it is a bit cumbersome to use. You cannot pass any object that responds to size to the method. You have to define an interface, and possibly include that interface in every class where you will be passing instances of the class to the method. Where and how you define these interfaces depends on the static typing system.
To explore what statically typing the obj_size1 example would look like, let's take the example and wrap it in a class named A:
class A
def obj_size1(obj) = 1 + obj.size
end
We'll add some classes that will be used when calling the method, so that the static type checkers have something to type check. We'll want to include an example that tests Ruby's numeric coercion protocol. In the example below, D#size...
Change the font size
Change margin width
Change background colour