Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

highlighting

  • Loading branch information...
commit 052e62251f7061a1398fb2a5483e55448407d1c0 1 parent 5c5f4e2
@egonSchiele authored
Showing with 20 additions and 16 deletions.
  1. +20 −16 TUTORIAL.md
View
36 TUTORIAL.md
@@ -387,26 +387,30 @@ If `failure_callback` returns `false`, the method that the contract is guarding
You can use contracts for method overloading! For example, here's a factorial function without method overloading:
- Contract Num => Num
- def fact x
- if x == 1
- x
- else
- x * fact(x - 1)
- end
- end
+```ruby
+Contract Num => Num
+def fact x
+ if x == 1
+ x
+ else
+ x * fact(x - 1)
+ end
+end
+```
Here it is again, re-written with method overloading:
- Contract 1 => 1
- def fact x
- x
- end
+```ruby
+Contract 1 => 1
+def fact x
+ x
+end
- Contract Num => Num
- def fact x
- x * fact(x - 1)
- end
+Contract Num => Num
+def fact x
+ x * fact(x - 1)
+end
+```
For an argument, each function will be tried in order. The first function that doesn't raise a `ContractError` will be used. So in this case, if x == 1, the first function will be used. For all other values, the second function will be used.
Please sign in to comment.
Something went wrong with that request. Please try again.