Ruby doesn't have method overloading, so you basically can't do that
and unless you have a really nice reason to, you should not try to do
it at all. Just create another method with a different name.
If you think you have a really awesome reason to do that, you could
follow the (ugly) implementation for the "[]" method in Array (
https://github.com/ruby/ruby/blob/017f0ffe1822264a8ead114f0b6055805185c0ac/array.c#L1164
).
Your implementation would then look like:
class Grid
attr_accessor :cells
def cell(*args)
case args.size
when 1
puts "return via ordinal position"
when 2
puts "return via x,y coordinate"
end
end
end
The * is equivalent to the "params" keyword in C#. Not ideal, but
that's as close as you will probably get to method overloading.
-
Maurício Linhares
http://mauricio.github.io/ -
http://twitter.com/#!/mauriciojr