Instead of supporting method overloading Ruby overwrites existing methods. Can anyone explain why the language was designed this way?
Instead of supporting method overloading Ruby overwrites existing methods. Can anyone explain why the
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Method overloading can be achieved by declaring two methods with the same name and different signatures. These different signatures can be either,
method(int a, int b) vs method(String a, String b)method(a) vs method(a, b)We cannot achieve method overloading using the first way because there is no data type declaration in ruby(dynamic typed language). So the only way to define the above method is
def(a,b)With the second option, it might look like we can achieve method overloading, but we can’t. Let say I have two methods with different number of arguments,
So ruby needs to maintain one method in the method look up chain with a unique name.