• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doesn't Ruby have overloaded methods?

 
Ranch Hand
Posts: 701
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


def digaMoo
puts 'Moooo'
end

def digaMoo numeroDeMoos
puts 'mooooooo...'*numeroDeMoos
end


digaMoo


digaMoo 5




The code above doesn't work using SciTE. Doesn't Ruby accept overloaded methods like Java?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - how could it? In Java, overloading depends on the declared type of each parameter. In Ruby, parameters have no declared type. I suppose that the Ruby interpreter could allow overloading based only on the number of parameters, but it doesn't. Probably Matz didn't see a benefit to that, and so he kept a simpler model: there is only one method named digaMoo at a time, and when you declare the second version, it simply replaces the first.

I think the Ruby Way to handle this is to simply give the methods different names... like say_moo and say_moo_n_times, where the first expects no parameters, and the second expects one numeric parameter.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Ruby you've got something even better than overloading: optional parameters!!!

You get the same functionality with less code, less repetition, and less chance for icky bugs!
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
+1 for Mark

Ruby follows a different sort of OO implementation than Java. In Ruby, *everything* is an object. You don't have pseudo-objects like you do in java (int, char, long).

You communicate with objects via messages -- you send an object the message, either explicitly like foo.bar or implicitly like foo.send(:bar). The arguments are passed (ultimately) as an array. Most of the time you don't need to think of them being passed as such, but it's nice to have available, if needed. Since they're passed as an array, Ruby doesn't really care too much how many arguments are passed (unless you've declared that there are one or more arguments which do not have a default value).

Having the default value, like shown above, really makes more sense to me than overloading. I only need to look in one method to understand what's happening and I can easily create a default behaviour.

When Java came out, I thought it was great. I taught it for Sun for a while, too. My "dayjob" still uses it (at least a chunk of the time). But I've found Ruby to be "fun" and to increase my pleasure in coding.....
 
Author
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really need something like overloading (even though I would recommend against is usually) you can sort of fake it by taking a hash of arguments:


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic