[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Constructor overloading???

Panagiotis Karvounis

2/21/2005 9:04:00 PM

Hi,

Does Ruby support constructor overloading?

If yes,could someone help me?

Thanks


7 Answers

Austin Ziegler

2/21/2005 9:21:00 PM

0

On Tue, 22 Feb 2005 06:04:07 +0900, Panagiotis Karvounis
<pkarvou@gmail.com> wrote:
> Does Ruby support constructor overloading?
>
> If yes,could someone help me?

Ruby does not support method overloading at all, only overriding.
Generally, it doesn't need it, I find.

What problem are you trying to solve? We can probably help you out
more with that.

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


ruby talk

2/21/2005 9:31:00 PM

0

On Tue, 22 Feb 2005 06:04:07 +0900, Panagiotis Karvounis
<pkarvou@gmail.com> wrote:
> Hi,
>
> Does Ruby support constructor overloading?

No; Ruby desn't support method overloading. But you can pass a
variable number of arguments and do some logic based on what's given.

One approach is to define all the args and set default values:

def initialize ( x, y="Hey!", z=nil )
end


Another might be to just accept any number of args as an array:

def initialize ( *args)
# The array args now has the arguments
end

Or combine them

def initialize (x=0, y="Hey!", *z=nil )
end

Or use a hash as the primary argument:

def initialize ( h={} )
# Now go look for named arguments in the hash
end

f = Foo.new( :user_name => "Jimbo!", :country => "Bronx" )

What are you trying to accomplish?


James


Eliah Hecht

2/22/2005 5:22:00 PM

0

I've run into this before when (for instance) I want initialize() to
do one thing if it gets one arg, and a totally different thing if it
gets two args. AFAIK, the only way to do this is to bundle the args up
in an array, and to look at the size of that, which just feels clumsy.

-Eliah.


On Tue, 22 Feb 2005 06:31:00 +0900, James G. Britt
<ruby.talk.list@gmail.com> wrote:
> On Tue, 22 Feb 2005 06:04:07 +0900, Panagiotis Karvounis
> <pkarvou@gmail.com> wrote:
> > Hi,
> >
> > Does Ruby support constructor overloading?
>
> No; Ruby desn't support method overloading. But you can pass a
> variable number of arguments and do some logic based on what's given.
>
> One approach is to define all the args and set default values:
>
> def initialize ( x, y="Hey!", z=nil )
> end
>
> Another might be to just accept any number of args as an array:
>
> def initialize ( *args)
> # The array args now has the arguments
> end
>
> Or combine them
>
> def initialize (x=0, y="Hey!", *z=nil )
> end
>
> Or use a hash as the primary argument:
>
> def initialize ( h={} )
> # Now go look for named arguments in the hash
> end
>
> f = Foo.new( :user_name => "Jimbo!", :country => "Bronx" )
>
> What are you trying to accomplish?
>
>
> James
>
>


ruby talk

2/22/2005 7:01:00 PM

0

On Wed, 23 Feb 2005 02:22:15 +0900, Eliah Hecht <eliahhecht@gmail.com> wrote:
> I've run into this before when (for instance) I want initialize() to
> do one thing if it gets one arg, and a totally different thing if it
> gets two args. AFAIK, the only way to do this is to bundle the args up
> in an array, and to look at the size of that, which just feels clumsy.

You may do better using a hash and named arguments. The
initialization logic at least should be cleaner than " if ary.size >
x ".

Plus you get greater flexibility in argument passing (for example,
accommodating new arguments) without break order or arg-count
dependant code.


James


Brian Schröder

2/22/2005 8:00:00 PM

0

On Wed, 23 Feb 2005 02:22:15 +0900, Eliah Hecht <eliahhecht@gmail.com> wrote:
> I've run into this before when (for instance) I want initialize() to
> do one thing if it gets one arg, and a totally different thing if it
> gets two args. AFAIK, the only way to do this is to bundle the args up
> in an array, and to look at the size of that, which just feels clumsy.
>
> -Eliah.
If the initializers are totally different, then it may make sense to
create two new differently named constructors.

I think that makes the program better readable.

Greetings,

Brian
>
> On Tue, 22 Feb 2005 06:31:00 +0900, James G. Britt
> <ruby.talk.list@gmail.com> wrote:
> > On Tue, 22 Feb 2005 06:04:07 +0900, Panagiotis Karvounis
> > <pkarvou@gmail.com> wrote:
> > > Hi,
> > >
> > > Does Ruby support constructor overloading?
> >
> > No; Ruby desn't support method overloading. But you can pass a
> > variable number of arguments and do some logic based on what's given.
> >
> > One approach is to define all the args and set default values:
> >
> > def initialize ( x, y="Hey!", z=nil )
> > end
> >
> > Another might be to just accept any number of args as an array:
> >
> > def initialize ( *args)
> > # The array args now has the arguments
> > end
> >
> > Or combine them
> >
> > def initialize (x=0, y="Hey!", *z=nil )
> > end
> >
> > Or use a hash as the primary argument:
> >
> > def initialize ( h={} )
> > # Now go look for named arguments in the hash
> > end
> >
> > f = Foo.new( :user_name => "Jimbo!", :country => "Bronx" )
> >
> > What are you trying to accomplish?
> >
> >
> > James
> >
> >
>
>


--
--
Brian Schröder
http://ruby.brian-sch...



Panagiotis Karvounis

2/22/2005 9:50:00 PM

0

Thanks!!!!!!!!!!


On Wed, 23 Feb 2005 04:59:42 +0900, Brian Schröder <ruby.brian@gmail.com> wrote:
> On Wed, 23 Feb 2005 02:22:15 +0900, Eliah Hecht <eliahhecht@gmail.com> wrote:
> > I've run into this before when (for instance) I want initialize() to
> > do one thing if it gets one arg, and a totally different thing if it
> > gets two args. AFAIK, the only way to do this is to bundle the args up
> > in an array, and to look at the size of that, which just feels clumsy.
> >
> > -Eliah.
> If the initializers are totally different, then it may make sense to
> create two new differently named constructors.
>
> I think that makes the program better readable.
>
> Greetings,
>
> Brian
> >
> > On Tue, 22 Feb 2005 06:31:00 +0900, James G. Britt
> > <ruby.talk.list@gmail.com> wrote:
> > > On Tue, 22 Feb 2005 06:04:07 +0900, Panagiotis Karvounis
> > > <pkarvou@gmail.com> wrote:
> > > > Hi,
> > > >
> > > > Does Ruby support constructor overloading?
> > >
> > > No; Ruby desn't support method overloading. But you can pass a
> > > variable number of arguments and do some logic based on what's given.
> > >
> > > One approach is to define all the args and set default values:
> > >
> > > def initialize ( x, y="Hey!", z=nil )
> > > end
> > >
> > > Another might be to just accept any number of args as an array:
> > >
> > > def initialize ( *args)
> > > # The array args now has the arguments
> > > end
> > >
> > > Or combine them
> > >
> > > def initialize (x=0, y="Hey!", *z=nil )
> > > end
> > >
> > > Or use a hash as the primary argument:
> > >
> > > def initialize ( h={} )
> > > # Now go look for named arguments in the hash
> > > end
> > >
> > > f = Foo.new( :user_name => "Jimbo!", :country => "Bronx" )
> > >
> > > What are you trying to accomplish?
> > >
> > >
> > > James
> > >
> > >
> >
> >
>
> --
> --
> Brian Schröder
> http://ruby.brian-sch...
>
>



Robert Klemme

2/23/2005 4:30:00 PM

0


"Panagiotis Karvounis" <pkarvou@gmail.com> schrieb im Newsbeitrag
news:3f57fbe10502211303395b3e6d@mail.gmail.com...
> Hi,
>
> Does Ruby support constructor overloading?
>
> If yes,could someone help me?
>
> Thanks
>
As others have pointed out there is no overloading in Ruby. However, if
you really need to you can mimic it. See
http://www.rubygarden.org/ruby?MethodO...
http://www.rubygarden.org/ruby?R...

Regards

robert