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.
# 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?
I find that overriding is much more powerful. You can override just 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 also requires static typing I think.
Einar Magnús Boson
+354-661 1649
einarmagnus@tistron.se
einar.boson@gmail.com
···
On 17.11.2008, at 07:50 , Zhao Yi wrote:
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/\.
From: Zhao Yi [mailto:youhaodeyi@gmail.com]
# 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
From: Zhao Yi [mailto:youhaodeyi@gmail.com]
#
# 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
try it like,
def build directory=nil
cd directory unless directory.nil?
make
end
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
Call it: buildWithDirectory(directory)
but without overloading, I have to do this:
def build(directory)
if(directory == nil)
make
else
cd directory
make
endif
end
pjb@informatimago.com (Pascal J. Bourguignon) writes:
Zhao Yi <youhaodeyi@gmail.com> writes:
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
Call it: buildWithDirectory(directory)
build_with_directory would probably be a more conventional Ruby function name.
It's interesting that without the feature of overloading the code is both shorter and more readable.
···
On 17 nov. 08, at 03:05, Peña, Botp wrote:
From: Zhao Yi [mailto:youhaodeyi@gmail.com]
# 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
try it like,
def build directory=nil
cd directory unless directory.nil?
make
end
def build(directory = nil)
cd directory if directory
make
end
···
2008/11/17 Brian Adkins <lojicdotcom@gmail.com>
pjb@informatimago.com (Pascal J. Bourguignon) writes:
> Zhao Yi <youhaodeyi@gmail.com> writes:
>> 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
>
> Call it: buildWithDirectory(directory)
build_with_directory would probably be a more conventional Ruby function
name.
Well, default arguments are a different matter really. That's all that
is needed in this example case, fair enough. But in more complex cases
overloading can be advantageous because it allows for code to be split
up into smaller, more manageable chunks. But Ruby would probably need
to support it internally for the feature to be especially beneficial.
Hmm... I suppose that would require Ruby a bit more like Duby?
It's interesting that without the feature of overloading the code is both shorter and more readable.
Well, default arguments are a different matter really. That's all that
is needed in this example case, fair enough. But in more complex cases
overloading can be advantageous because it allows for code to be split
up into smaller, more manageable chunks. But Ruby would probably need
to support it internally for the feature to be especially beneficial.
Hmm... I suppose that would require Ruby a bit more like Duby?
I know this will not work in every case, but can't you check the type of input in a function like foo(x) and convert it to the desired form or perform the required operation? I agree that this adds more code, and probably more places for errors, but it places all the code for foo in one place. I used this when making my own version of rational(). It would accept one or two integers, floats, strings, or rational numbers, or combination of them. I only had to change initialize without having to copy all the other code to methods like rational_f(), rational_i(), etc.
···
On Nov 17, 2:22 pm, Joe Wölfel <j...@talkhouse.com> wrote: