We can define a become_older
method that accepts a number indicating the years to grow:
classPersongetter:agedefinitialize(@name : String, @age : Int=0) enddefbecome_older@age+=1enddefbecome_older(years) @age+= years endend john =Person.new "John" john.age # => 0 john.become_older john.age # => 1 john.become_older 5 john.age # => 6
That is, you can have different methods with the same name and different number of parameters and they will be considered as separate methods. This is called method overloading.
Methods overload by several criteria:
- The number of parameters
- The type restrictions applied to parameters
- The names of required named parameters
- Whether the method accepts a block or not
For example, we can define four different become_older
methods:
classPerson@age=0# Increases age by onedefbecome_older@age+=1end# Increases age by the given number of yearsdefbecome_older(years : Int32) @age+= years end# Increases age by the given number of years, as a Stringdefbecome_older(years : String) @age+= years.to_i end# Yields the current age of this person and increases# its age by the value returned by the blockdefbecome_older(&) @age+=yield@ageendend person =Person.new "John" person.become_older person.age # => 1 person.become_older 5 person.age # => 6 person.become_older "12" person.age # => 18 person.become_older do |current_age| current_age <20?10 : 30end person.age # => 28
Note that in the case of the method that yields, the compiler figured this out because there's a yield
expression. To make this more explicit, you can add a dummy &block
parameter at the end:
classPerson@age=0defbecome_older(&block) @age+=yield@ageendend
In generated documentation the dummy &block
method will always appear, regardless of you writing it or not.
Given the same number of parameters, the compiler will try to sort them by leaving the less restrictive ones to the end:
classPerson@age=0# First, this method is defineddefbecome_older(age) @age+= age end# Since "String" is more restrictive than no restriction# at all, the compiler puts this method before the previous# one when considering which overload matches.defbecome_older(age : String) @age+= age.to_i endend person =Person.new "John"# Invokes the first definition person.become_older 20# Invokes the second definition person.become_older "12"
However, the compiler cannot always figure out the order because there isn't always a total ordering, so it's always better to put less restrictive methods at the end.