Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How can I overload a method in Ruby

Reply
Thread Tools

How can I overload a method in Ruby

 
 
Zhao Yi
Guest
Posts: n/a
 
      11-17-2008
This is my class definition:
class Impl
include MyModule
def fun()
puts ("fun")
end

def fun(directory)
puts ("fun directory")
end
end
m=Impl.new()
m.fun()

When I run this program, I got this error:

class_objects/module.rb:22:in `fun': wrong number of arguments (0 for 1)
(ArgumentError)
from class_objects/module.rb:22

I declared two methods with the same name but different parameters. Why
can't ruby support overload method?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      11-17-2008

On Nov 16, 2008, at 23:26 , Zhao Yi wrote:

short answer: you can't (via normal ruby).

> When I run this program, I got this error:
>
> class_objects/module.rb:22:in `fun': wrong number of arguments (0
> for 1)
> (ArgumentError)
> from class_objects/module.rb:22
>
> I declared two methods with the same name but different parameters.
> Why
> can't ruby support overload method?


because it wasn't designed to, that's why.

You can use default args to do what you want above:

> def fun dir = nil
> puts "fun #{dir}"
> end




 
Reply With Quote
 
 
 
 
Einar Magns Boson
Guest
Posts: n/a
 
      11-17-2008
[Note: parts of this message were removed to make it a legal post.]

there is no overloading, only overriding.


You can have variable number of arguments by writing

def fun(*params)
#here params is an array of your arguments, [] if none.
end

you can also have a default value for your parameter:

class Impl
include MyModule

def fun(directory = "")
puts "fun #{directory}"
end

end
m=Impl.new()
m.fun()
# >> fun
m.fun "/dev/null"
# >> fun /dev/null"

On 17.11.2008, at 07:26 , Zhao Yi wrote:

> This is my class definition:
> class Impl
> include MyModule
> def fun()
> puts ("fun")
> end
>
> def fun(directory)
> puts ("fun directory")
> end
> end
> m=Impl.new()
> m.fun()
>
> When I run this program, I got this error:
>
> class_objects/module.rb:22:in `fun': wrong number of arguments (0
> for 1)
> (ArgumentError)
> from class_objects/module.rb:22
>
> I declared two methods with the same name but different parameters.
> Why
> can't ruby support overload method?
> --
> Posted via http://www.ruby-forum.com/.
>



 
Reply With Quote
 
Zhao Yi
Guest
Posts: n/a
 
      11-17-2008
Ryan Davis wrote:
> On Nov 16, 2008, at 23:26 , Zhao Yi wrote:
>
> short answer: you can't (via normal ruby).
>
>> When I run this program, I got this error:
>>
>> class_objects/module.rb:22:in `fun': wrong number of arguments (0
>> for 1)
>> (ArgumentError)
>> from class_objects/module.rb:22
>>
>> I declared two methods with the same name but different parameters.
>> Why
>> can't ruby support overload method?

>
> because it wasn't designed to, that's why.
>
> You can use default args to do what you want above:


I am new to Ruby. What's do you mean by default args?
thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Zhao Yi
Guest
Posts: n/a
 
      11-17-2008
Einar Magnús Boson wrote:
> there is no overloading, only overriding.
>
>
> You can have variable number of arguments by writing
>
> def fun(*params)
> #here params is an array of your arguments, [] if none.
> end
>
> you can also have a default value for your parameter:
>
> class Impl
> include MyModule
>
> def fun(directory = "")
> puts "fun #{directory}"
> end
>
> end
> m=Impl.new()
> m.fun()
> # >> fun
> m.fun "/dev/null"
> # >> fun /dev/null"


ok I understand. Just curious, overload is a good feature in OO
programming. Why doesn't Ruby support it? If I use the method you
mentioned, I have to check the parameters and use if - else or swith to
distinguish them.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      11-17-2008
RnJvbTogWmhhbyBZaSBbbWFpbHRvOnlvdWhhb2RleWlAZ21haW wuY29tXSANCiMgb2sgSSB1bmRl
cnN0YW5kLiBKdXN0IGN1cmlvdXMsIG92ZXJsb2FkIGlzIGEgZ2 9vZCBmZWF0dXJlIGluIE9PIA0K
IyBwcm9ncmFtbWluZy4gV2h5IGRvZXNuJ3QgUnVieSBzdXBwb3 J0IGl0PyBJZiBJIHVzZSB0aGUg
bWV0aG9kIHlvdSANCiMgbWVudGlvbmVkLCBJIGhhdmUgdG8gY2 hlY2sgdGhlIHBhcmFtZXRlcnMg
YW5kIHVzZSBpZiAtIGVsc2UgDQojIG9yIHN3aXRoIHRvIGRpc3 Rpbmd1aXNoIHRoZW0uDQoNCm1h
eWJlIGJlY2F1c2Ugb2YgdGhlIGZmIGRpbGVtbWE6DQoNCmRlZi BtKDEpDQplbmQNCg0KZGVmIG0o
Im9uZSIpDQplbmQNCg0KDQpydWJ5IHdpbGwgYmUgZm9yY2VkIH RvIGNoZWNrIHR5cGUuLi4gd2Mg
bGVhZHMgdG8gc3RhdGljIHR5cGluZy4uLiB3YyBpcyBub3QgbW F0eiBydWJ5Li4NCg0KYnR3LCBa
aGFvLCBjYW4geW91IGdpdmUgdXMgYSByZWFsIGNhc2Ugd2hlcm UgeW91IHJlYWxseSBuZWVkIHRv
IGRvIG1ldGhvZCBsb2FkaW5nPw0KDQpraW5kIHJlZ2FyZHMgLW JvdHANCg==

 
Reply With Quote
 
Einar Magns Boson
Guest
Posts: n/a
 
      11-17-2008
I find that overriding is much more powerful. You can override just =20
about any method.
Try

class Fixnum
def to_s
"this is kinda stupid"
end
end

puts 5

Overloading is mostly used for default arguments anyways isn't it? and =20=

also requires static typing I think.



Einar Magn=FAs Boson
+354-661 1649
http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail Removed)

On 17.11.2008, at 07:50 , Zhao Yi wrote:

> Einar Magn=FAs Boson wrote:
>> there is no overloading, only overriding.
>>
>>
>> You can have variable number of arguments by writing
>>
>> def fun(*params)
>> #here params is an array of your arguments, [] if none.
>> end
>>
>> you can also have a default value for your parameter:
>>
>> class Impl
>> include MyModule
>>
>> def fun(directory =3D "")
>> puts "fun #{directory}"
>> end
>>
>> end
>> m=3DImpl.new()
>> m.fun()
>> # >> fun
>> m.fun "/dev/null"
>> # >> fun /dev/null"

>
> ok I understand. Just curious, overload is a good feature in OO
> programming. Why doesn't Ruby support it? If I use the method you
> mentioned, I have to check the parameters and use if - else or swith =20=


> to
> distinguish them.
> --=20
> Posted via http://www.ruby-forum.com/.
>



 
Reply With Quote
 
Zhao Yi
Guest
Posts: n/a
 
      11-17-2008
Peña, Botp wrote:
> From: Zhao Yi [(E-Mail Removed)]
> # ok I understand. Just curious, overload is a good feature in OO
> # programming. Why doesn't Ruby support it? If I use the method you
> # mentioned, I have to check the parameters and use if - else
> # or swith to distinguish them.
>
> maybe because of the ff dilemma:
>
> def m(1)
> end
>
> def m("one")
> end
>
>
> ruby will be forced to check type... wc leads to static typing... wc is
> not matz ruby..
>
> btw, Zhao, can you give us a real case where you really need to do
> method loading?
>
> kind regards -botp


I want to write a build method which will build source code in a
directory. I assume there are two build methods, one doesn't have
parameter while the other have one parameter. The method without
parameter will build the source code in default directory, the method
with one parameter will build the source code in the directory which is
specified by this parameter. With method overloading, I can write them
as below:

def build()
make
end

def build(directory)
cd directory
make
end

but without overloading, I have to do this:
def build(directory)
if(directory == nil)
make
else
cd directory
make
endif
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      11-17-2008
RnJvbTogWmhhbyBZaSBbbWFpbHRvOnlvdWhhb2RleWlAZ21haW wuY29tXSANCiMgZGVmIGJ1aWxk
KCkNCiMgICAgIG1ha2UNCiMgZW5kDQojIA0KIyBkZWYgYnVpbG QoZGlyZWN0b3J5KQ0KIyAgICAg
Y2QgZGlyZWN0b3J5DQojICAgICBtYWtlDQojIGVuZA0KIyANCi MgYnV0IHdpdGhvdXQgb3Zlcmxv
YWRpbmcsIEkgaGF2ZSB0byBkbyB0aGlzOg0KIyBkZWYgYnVpbG QoZGlyZWN0b3J5KQ0KIyAgICAg
aWYoZGlyZWN0b3J5ID09IG5pbCkNCiMgICAgICAgbWFrZQ0KIy AgICAgZWxzZQ0KIyAgICAgICBj
ZCBkaXJlY3RvcnkNCiMgICAgICAgbWFrZQ0KIyAgICAgZW5kaW YNCiMgZW5kDQoNCnRyeSBpdCBs
aWtlLA0KDQpkZWYgYnVpbGQgZGlyZWN0b3J5PW5pbA0KICBjZC BkaXJlY3RvcnkgdW5sZXNzIGRp
cmVjdG9yeS5uaWw/DQogIG1ha2UNCmVuZA0K

 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      11-17-2008
RnJvbTogUGXDsWEsIEJvdHAgW21haWx0bzpib3RwQGRlbG1vbn RlLXBoaWwuY29tXSANCiMgZGVm
IGJ1aWxkIGRpcmVjdG9yeT1uaWwNCiMgICBjZCBkaXJlY3Rvcn kgdW5sZXNzIGRpcmVjdG9yeS5u
aWw/DQojICAgbWFrZQ0KIyBlbmQNCg0KDQpvciBpZiB5b3UgZG8gbm 90IGxpa2UgdGhlIGlmL3Vu
bGVzcyBwYXJ0LA0KDQpkZWYgYnVpbGQgZGlyZWN0b3J5PSIuIg 0KICBjZCBkaXJlY3RvcnkNCiAg
bWFrZQ0KZW5kDQoNCg0KDQogDQo=

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
first time trying to overload a method/member Fx --need help ofranko C++ 3 01-07-2009 12:11 AM
Cannot overload virtual method? Allen C++ 2 08-27-2008 04:23 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments