Ask a Question related to Ruby, Design and Development.
-
Dimitrios Galanakis #1
newbie question: function overloading
I need to define a method that performs differently when operated on objects
of different type (overloading). Currently I use various if's to check for
the type of the object as follows:
def somefunction(a)
if a.kind_of?(someType)
expression1
return
end
if a.kind_of(someOtherType)
expression2
return
end
i am wondering if there is a simpler way to implement that without the if's,
in a way similar to C++ overloading. I mean I would like to define two
functions with the same name that will somehow be aware of the type of
their argument...
sincerely,
DG
Dimitrios Galanakis Guest
-
Newbie Question: How to Move Div in Spry Grow function
So, this is probably a question that will result in a facepalm to my own head, but how do make a div move to the top left corner of a container div... -
#39361 [NEW]: mbstring function overloading - local value ignored
From: christoph at ziegenberg dot de Operating system: Win XP SP 2 PHP version: 5.2.0 PHP Bug Type: mbstring related Bug... -
Creating Function Newbie question
Hi I have written a small bit of code for my web site that basically is anti hack code. $checking = ($HTTP_GET_VARS); if (ereg ("+",... -
Newbie Question: Can PHP communicate with a C function/program
I have several library functions that have been created in 'C' which I would prefer not to have to re-write all of them to PHP. Is there a way to... -
[PHP] OO function overloading?
Hi, This statement isn't entirely correct, overloading is possible with the overload extension. http://www.php.net/overload Regards, Greg... -
Chad Fowler #2
Re: newbie question: function overloading
On Fri, 3 Oct 2003, Dimitrios Galanakis wrote:
# I need to define a method that performs differently when operated on objects
# of different type (overloading). Currently I use various if's to check for
# the type of the object as follows:
#
There are really two answers to this:
1) Ryan Pavlik has created a library that provides overloading capability
like this. You could give it a try and it might feel right to your C++
ways.
2) I would lean more heavily toward this one: The Ruby Way to accomplish
what you're trying to accomplish very likely isn't to check type in this
way. I remember coming to Ruby from C++/Java and wondering why I couldn't
do method overloading like this. My brain was wired into the static
typing way of doing things and I even felt like overloading was almost
a necessary part of OO. It didn't take long before I stopped feeling the
need for this capability as I started reading existing code and
understanding the Ruby idioms better.
Maybe if you could post a more specific example/statement of what you're
trying to accomplish, we could see about helping you with option #2.
Chad
Chad Fowler Guest
-
Dimitrios Galanakis #3
Re: newbie question: function overloading
I was expecting an answer like that... I wanted to ask then if operator
overloading if exactly similar to function overloading.
I would like for example to define my own complex class. In this canse
the * operator will perform differencly when multiplying a real and a
complex and when multiplying two complex numbers (this is almost what I
need to do only the difference is that instead of a complex class I have
some other class for which it makes sence to multiply objects of that
class together and with real numbers). In other words how can I simplify
the following code by removing the ifs?
class complex
def *(something)
result=self.dup
if something.kind_of?(Numeric)
result.real*=something
result.imag*=something
return result
end
if something.kind_of(complex)
result.real=result.real*something.real-result.imag*something.imag
... etc etc etc
end
end
end
Chad Fowler wrote:> On Fri, 3 Oct 2003, Dimitrios Galanakis wrote:
>
> # I need to define a method that performs differently when operated on objects
> # of different type (overloading). Currently I use various if's to check for
> # the type of the object as follows:
> #
>
> There are really two answers to this:
>
> 1) Ryan Pavlik has created a library that provides overloading capability
> like this. You could give it a try and it might feel right to your C++
> ways.
> 2) I would lean more heavily toward this one: The Ruby Way to accomplish
> what you're trying to accomplish very likely isn't to check type in this
> way. I remember coming to Ruby from C++/Java and wondering why I couldn't
> do method overloading like this. My brain was wired into the static
> typing way of doing things and I even felt like overloading was almost
> a necessary part of OO. It didn't take long before I stopped feeling the
> need for this capability as I started reading existing code and
> understanding the Ruby idioms better.
>
> Maybe if you could post a more specific example/statement of what you're
> trying to accomplish, we could see about helping you with option #2.
>
> Chad
>
>Dimitrios Galanakis Guest
-
Weirich, James #4
Re: newbie question: function overloading
> From: Dimitrios Galanakis [mailto:galanaki@uiuc.edu]
I would do it like this ...> I need to define a method that performs differently when
> operated on objects of different type (overloading).
> Currently I use various if's to check for the type of
> the object as follows:
>
> def somefunction(a)
> if a.kind_of?(someType)
> expression1
> return
> end
> if a.kind_of(someOtherType)
> expression2
> return
> end
> end
class SomeType
def somefunction
expression1
end
end
class SomeOtherType
def somefunction
expression2
end
end
Then just call:
a.somefunction
If you still feel the need to pass obj as an argument to somefunction, you
can always use:
def somefunction(a)
a.somefunction
end
--
-- Jim Weirich / Compuware
-- FWP Capture Services
-- Phone: 859-386-8855
Weirich, James Guest
-
Chad Fowler #5
Re: newbie question: function overloading
On Fri, 3 Oct 2003, Weirich, James wrote:
# I would do it like this ...
#
# class SomeType
# def somefunction
# expression1
# end
# end
#
# class SomeOtherType
# def somefunction
# expression2
# end
# end
#
# Then just call:
#
# a.somefunction
#
# If you still feel the need to pass obj as an argument to somefunction, you
# can always use:
#
# def somefunction(a)
# a.somefunction
# end
#
#
And if you'd like to see that diagrammed:
[url]http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html[/url]
:)
Chad Fowler Guest
-
Greg Vaughn #6
Re: newbie question: function overloading
Dimitrios Galanakis wrote:
I'm still a relative Ruby newbie, so I can't bang out any example code,> I was expecting an answer like that... I wanted to ask then if operator
> overloading if exactly similar to function overloading.
> I would like for example to define my own complex class. In this canse
> the * operator will perform differencly when multiplying a real and a
> complex and when multiplying two complex numbers (this is almost what I
> need to do only the difference is that instead of a complex class I have
> some other class for which it makes sence to multiply objects of that
> class together and with real numbers). In other words how can I simplify
> the following code by removing the ifs?
>
but I hope I can point out a couple of useful tidbits. In Ruby classes
aren't closed. That means you can add methods to Numeric whenever you
want. Also, as written, your code wouldn't work if you multiplied
Numeric * Complex
I'd make your new class a subclass of Numeric, and then modify the
Numeric class. Alias the old * to something else, and put your own
implementation in place. I still can't see how to get rid of an 'if',
but your new method can check if either element is Complex then call a
complex_multiply method, else use the aliased old *.
-Greg Vaughn
>
>
> class complex
> def *(something)
> result=self.dup
> if something.kind_of?(Numeric)
> result.real*=something
> result.imag*=something
> return result
> end
> if something.kind_of(complex)
>
> result.real=result.real*something.real-result.imag*something.imag
> ... etc etc etc
> end
>
> end
>
> end
Greg Vaughn Guest
-
Oliver Dain #7
Re: newbie question: function overloading
Dimitrios Galanakis wrote:
Check out the strongtyping module at RAA:> I need to define a method that performs differently when operated on
> objects of different type (overloading). Currently I use various
> if's to check for the type of the object as follows:
>
> def somefunction(a)
>
> if a.kind_of?(someType)
> expression1
> return
> end
>
> if a.kind_of(someOtherType)
> expression2
> return
>
> end
>
>
> i am wondering if there is a simpler way to implement that without
> the if's, in a way similar to C++ overloading. I mean I would like
> to define two functions with the same name that will somehow be
> aware of the type of their argument...
>
> sincerely,
>
> DG
[url]http://raa.ruby-lang.org/list.rhtml?name=strongtyping[/url]
Oliver Dain Guest
-
Austin Ziegler #8
Re: newbie question: function overloading
On Fri, 3 Oct 2003 06:14:45 +0900, Greg Vaughn wrote:
In this case, what is wanted is Numeric#coerce.> Dimitrios Galanakis wrote:> I'm still a relative Ruby newbie, so I can't bang out any example code,>> I was expecting an answer like that... I wanted to ask then if operator
>> overloading if exactly similar to function overloading. I would like
>> for example to define my own complex class. In this canse the *
>> operator will perform differencly when multiplying a real and a complex
>> and when multiplying two complex numbers (this is almost what I need to
>> do only the difference is that instead of a complex class I have some
>> other class for which it makes sence to multiply objects of that class
>> together and with real numbers). In other words how can I simplify the
>> following code by removing the ifs?
> but I hope I can point out a couple of useful tidbits. In Ruby classes
> aren't closed. That means you can add methods to Numeric whenever you
> want. Also, as written, your code wouldn't work if you multiplied Numeric
> * Complex
-austin
--
austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.10.02
* 18.37.33
Austin Ziegler Guest
-
Christoph #9
Re: newbie question: function overloading
"Dimitrios Galanakis" wrote:
....Short answer - you can't - the coerce frame-work is probably your best bet> class together and with real numbers). In other words how can I simplify
> the following code by removing the ifs?
in a single dispatched world. Expect to see more and more `ifs and elses"
the deeper you extend the Numeric class hierarchy. See
[url]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/51566[/url]
for an interesting snippet on Matz's thinking on the subject.
/Christoph
Christoph Guest
-
Jim Weirich #10
Re: newbie question: function overloading
On Thu, 2003-10-02 at 17:12, Chad Fowler wrote:
And in our particular instance, we have an even stronger reason to move> And if you'd like to see that diagrammed:
> [url]http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html[/url]
to polymorphism because the condition was based on a type (rather than
an enumeration in the diagram example).
--
-- Jim Weirich [email]jweirich@one.net[/email] [url]http://onestepback.org[/url]
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
Jim Weirich Guest
-
Robert Klemme #11
Re: newbie question: function overloading
"Dimitrios Galanakis" <galanaki@uiuc.edu> schrieb im Newsbeitrag
news:4i_eb.147$fm2.69960@vixen.cso.uiuc.edu...objects> I need to define a method that performs differently when operated onfor> of different type (overloading). Currently I use various if's to checkif's,> the type of the object as follows:
>
> def somefunction(a)
>
> if a.kind_of?(someType)
> expression1
> return
> end
>
> if a.kind_of(someOtherType)
> expression2
> return
>
> end
>
>
> i am wondering if there is a simpler way to implement that without theYou can find further info here:> in a way similar to C++ overloading. I mean I would like to define two
> functions with the same name that will somehow be aware of the type of
> their argument...
[url]http://www.rubygarden.org/ruby?MethodOverloading[/url]
This might be of interest, too:
[url]http://www.rubygarden.org/ruby?KeywordArguments[/url]
Regards
robert
Robert Klemme Guest



Reply With Quote

