Created
October 29, 2017 20:04
-
-
Save scalp42/c06281d476d2222b4715ec595c4f80cb to your computer and use it in GitHub Desktop.
Multiple types overloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add(x : Number, y : Number) | |
x + y | |
end | |
def add(x: Number, y: String) | |
x.to_s + y | |
end | |
# vs | |
def add(x : Number, y : [Number, String]) | |
if y.is_a?(String) | |
x.to_s + y | |
else | |
x + y | |
end | |
add(1, 2) | |
add (1, "a") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment