You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: TUTORIAL.md
+14-14
Original file line number
Diff line number
Diff line change
@@ -80,16 +80,16 @@ contracts.ruby comes with a lot of built-in contracts, including the following:
80
80
81
81
* Logical combinations
82
82
*[`Maybe`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Maybe) – specifies that a value _may be_ nil, e.g. `Maybe[String]` (equivalent to `Or[String,nil]`)
83
-
*[`Or`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Or) – passes if any of the given contracts pass, e.g. `Or[Fixnum, Float]`
84
-
*[`Xor`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Xor) – passes if exactly one of the given contracts pass, e.g. `Xor[Fixnum, Float]`
83
+
*[`Or`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Or) – passes if any of the given contracts pass, e.g. `Or[Integer, Float]`
84
+
*[`Xor`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Xor) – passes if exactly one of the given contracts pass, e.g. `Xor[Integer, Float]`
85
85
*[`And`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/And) – passes if all contracts pass, e.g. `And[Nat, -> (n) { n.even? }]`
86
86
*[`Not`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Not) – passes if all contracts fail for the given argument, e.g. `Not[nil]`
87
87
88
88
* Collections
89
89
*[`ArrayOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/ArrayOf) – checks that the argument is an array, and all elements pass the given contract, e.g. `ArrayOf[Num]`
90
90
*[`SetOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/SetOf) – checks that the argument is a set, and all elements pass the given contract, e.g. `SetOf[Num]`
91
91
*[`HashOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/HashOf) – checks that the argument is a hash, and all keys and values pass the given contract, e.g. `HashOf[Symbol => String]` or `HashOf[Symbol,String]`
92
-
*[`StrictHash`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/StrictHash) – checks that the argument is a hash, and every key passed is present in the given contract, e.g. `StrictHash[{ :description => String, :number => Fixnum }]`
92
+
*[`StrictHash`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/StrictHash) – checks that the argument is a hash, and every key passed is present in the given contract, e.g. `StrictHash[{ :description => String, :number => Integer }]`
93
93
*[`RangeOf`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/RangeOf) – checks that the argument is a range whose elements (#first and #last) pass the given contract, e.g. `RangeOf[Date]`
94
94
*[`Enum`](http://www.rubydoc.info/gems/contracts/Contracts/Builtin/Enum) – checks that the argument is part of a given collection of objects, e.g. `Enum[:a, :b, :c]`
95
95
@@ -152,7 +152,7 @@ end
152
152
153
153
You always need to specify a contract for the return value. In this example, `hello` doesn't return anything, so the contract is `nil`. Now you know that you can use a constant like `nil` as the end of a contract. Valid values for a contract are:
154
154
155
-
- the name of a class (like `String` or `Fixnum`)
155
+
- the name of a class (like `String` or `Integer`)
156
156
- a constant (like `nil` or `1`)
157
157
- a `Proc` that takes a value and returns true or false to indicate whether the contract passed or not
158
158
- a class that responds to the `valid?` class method (more on this later)
@@ -161,32 +161,32 @@ You always need to specify a contract for the return value. In this example, `he
Sometimes you want to be able to choose between a few contracts. `Or` takes a variable number of contracts and checks the argument against all of them. If it passes for any of the contracts, then the `Or` contract passes.
171
-
This introduces some new syntax. One of the valid values for a contract is an instance of a class that responds to the `valid?` method. This is what `Or[Fixnum, Float]` is. The longer way to write it would have been:
171
+
This introduces some new syntax. One of the valid values for a contract is an instance of a class that responds to the `valid?` method. This is what `Or[Integer, Float]` is. The longer way to write it would have been:
whichever you prefer. They both mean the same thing here: make a new instance of `Or` with `Fixnum` and `Float`. Use that instance to validate the argument.
189
+
whichever you prefer. They both mean the same thing here: make a new instance of `Or` with `Integer` and `Float`. Use that instance to validate the argument.
190
190
191
191
### A Product Function
192
192
@@ -455,7 +455,7 @@ Now you can use `Person` wherever you would have used `Or[Hash, nil]`. Your code
455
455
456
456
Contracts are very easy to define. To re-iterate, there are 5 kinds of contracts:
457
457
458
-
- the name of a class (like `String` or `Fixnum`)
458
+
- the name of a class (like `String` or `Integer`)
459
459
- a constant (like `nil` or `1`)
460
460
- a `Proc` that takes a value and returns true or false to indicate whether the contract passed or not
461
461
- a class that responds to the `valid?` class method (more on this later)
@@ -511,7 +511,7 @@ The `Or` contract takes a sequence of contracts, and passes if any of them pass.
511
511
This class inherits from `CallableClass`, which allows us to use `[]` when using the class:
512
512
513
513
```ruby
514
-
ContractC::Or[Fixnum, Float] => C::Num
514
+
ContractC::Or[Integer, Float] => C::Num
515
515
defdouble(x)
516
516
2* x
517
517
end
@@ -520,7 +520,7 @@ end
520
520
Without `CallableClass`, we would have to use `.new` instead:
521
521
522
522
```ruby
523
-
ContractC::Or.new(Fixnum, Float) => C::Num
523
+
ContractC::Or.new(Integer, Float) => C::Num
524
524
defdouble(x)
525
525
# etc
526
526
```
@@ -723,7 +723,7 @@ class MyBirthday < Struct.new(:day, :month)
0 commit comments