From: Gregory Brown Date: 2007-01-16T15:57:38+09:00 Subject: Re: How to realize method/function overloading in Ruby? On 1/16/07, Gregory Brown wrote: > On 1/16/07, Gregory Brown wrote: > > > > def do_something_with_array(a) > > > # assume a is an array > > > end > > > def do_something_with_hash(h) > > > # assume h is a hash > > > end > > > def do_something_with_int(i) > > > # assume i is an int > > > end Jeez! Sorry for the 3 emails, I must be tired. This is the little hack I was trying to demonstrate: seltzer:~/devel/parser-experiment/pegleg_evaluator sandal$ irb >> def foo_array(a) >> a + [1] >> end => nil >> def foo_string(a) >> a << "apple" >> end => nil >> def foo(a) >> send("#{a.class.name.downcase",a) >> end >> def foo(a) >> send("foo_#{a.class.name.downcase}",a) >> end => nil >> foo([1,2,3]) => [1, 2, 3, 1] >> foo("banana") => "bananaapple" Though as others mentioned, a lot of this can be avoided by clever design. :)