[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Is possible to define various methods at same time?

Iñaki Baz Castillo

4/6/2008 1:55:00 PM

Hi, is possible to define various methods in a single declaration? somethin=
g=20
as:

def method1, method2 (args)
...
end

Of course the above doesn't work, anyway to do it?

Thanks.


=2D-=20
I=C3=B1aki Baz Castillo

16 Answers

Phillip Gawlowski

4/6/2008 2:47:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Iñaki Baz Castillo wrote:
| Hi, is possible to define various methods in a single declaration?
something
| as:
|
| def method1, method2 (args)
| ...
| end
|
| Of course the above doesn't work, anyway to do it?
|
| Thanks.
|
|
Have you looked at #method_missing, and it's relatives, like
#instance_variable_set?

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan

~ It takes an uncommon mind to think of these things.
~ --- Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkf44nEACgkQbtAgaoJTgL9H7wCbBMIa4NJ2mjKmle2E3G9CwHgb
lhkAn3oPTijhiKTE0K+kEWAYS9zbQQJ4
=v7a6
-----END PGP SIGNATURE-----

Iñaki Baz Castillo

4/6/2008 2:56:00 PM

0

El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribi=C3=B3:

>
> Have you looked at #method_missing, and it's relatives, like
> #instance_variable_set?

Ok, but my purpose was in fact:

def method1, method2 (args)
...
super <-- so it call to "method1" or "method2" depending
...
end

I will try what you suggest. Thanks.

=2D-=20
I=C3=B1aki Baz Castillo

James Gray

4/6/2008 3:22:00 PM

0

On Apr 6, 2008, at 9:56 AM, I=F1aki Baz Castillo wrote:
> El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribi=F3:
>
>>
>> Have you looked at #method_missing, and it's relatives, like
>> #instance_variable_set?
>
> Ok, but my purpose was in fact:
>
> def method1, method2 (args)
> ...
> super <-- so it call to "method1" or "method2" depending
> ...
> end

Here's one way you might go about that:

class Parent
def example1
puts "In example 1."
end

def example2
puts "In example 2."
end
end

class Child < Parent
%w[1 2].each do |suffix|
define_method("example#{suffix}") do
puts "Forwarding to example #{suffix}..."
super
end
end
end

c =3D Child.new
c.example1
c.example2

__END__

Hope that helps.

James Edward Gray II=

Iñaki Baz Castillo

4/6/2008 10:43:00 PM

0

El Domingo, 6 de Abril de 2008, James Gray escribi=F3:
> On Apr 6, 2008, at 9:56 AM, I=F1aki Baz Castillo wrote:
> > El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribi=F3:
> >> Have you looked at #method_missing, and it's relatives, like
> >> #instance_variable_set?
> >
> > Ok, but my purpose was in fact:
> >
> > def method1, method2 (args)
> > ...
> > super <-- so it call to "method1" or "method2" depending
> > ...
> > end
>
> Here's one way you might go about that:
>
> class Parent
> def example1
> puts "In example 1."
> end
>
> def example2
> puts "In example 2."
> end
> end
>
> class Child < Parent
> %w[1 2].each do |suffix|
> define_method("example#{suffix}") do
> puts "Forwarding to example #{suffix}..."
> super
> end
> end
> end
>
> c =3D Child.new
> c.example1
> c.example2
>
> __END__
>
> Hope that helps.

Sure, thanks :)



=2D-=20
I=F1aki Baz Castillo

Robert Klemme

4/7/2008 11:31:00 AM

0

2008/4/7, I=F1aki Baz Castillo <ibc@aliax.net>:
> El Domingo, 6 de Abril de 2008, James Gray escribi=F3:
>
> > Hope that helps.
>
> Sure, thanks :)

Can you explain why you need this? Maybe there is a different
solution altogether. This redundancy in method definitions is
irritating to me and I suspect there might be a better way to achieve
what you really need.

Kind regards

robert

--=20
use.inject do |as, often| as.you_can - without end

Nobuyoshi Nakada

4/7/2008 7:04:00 PM

0

Hi,

At Sun, 6 Apr 2008 22:54:52 +0900,
I=F1aki Baz Castillo wrote in [ruby-talk:297234]:
> Hi, is possible to define various methods in a single declaration? someth=
ing=20
> as:
>=20
> def method1, method2 (args)
> ...
> end
>=20
> Of course the above doesn't work, anyway to do it?

Do you want alias?

def method1(args)
...
end
alias method2 method1

--=20
Nobu Nakada

Iñaki Baz Castillo

4/7/2008 8:38:00 PM

0

El Lunes, 7 de Abril de 2008, Robert Klemme escribi=F3:

> Can you explain why you need this? Maybe there is a different
> solution altogether. This redundancy in method definitions is
> irritating to me and I suspect there might be a better way to achieve
> what you really need.

Thanks a lot. Finally it was not so important for me as I thought initially=
,=20
so I can avoid using it. I asked that for the following reason:

I'm using CusomLogger class that inherits from Logger class. And I want to=
=20
modify some methods, all exactly the same:

def debug(log_zone, msg) super(formatter(log_zone, msg)) end
def info(log_zone, msg) super(formatter(log_zone, msg)) end
def warn(log_zone, msg) super(formatter(log_zone, msg)) end
def error(log_zone, msg) super(formatter(log_zone, msg)) end
def fatal(log_zone, msg) super(formatter(log_zone, msg)) end
def unknown(log_zone, msg) super(formatter(log_zone, msg)) end

As you can see, all the methods body is exactly the same. I expected there=
=20
could be a cool way to do something as:

def debug,info,war,error,fatal,unknown(log_zone, msg)
super(formatter(log_zone, msg))
end

just it.

Thanks a lot for your help.




=2D-=20
I=F1aki Baz Castillo

Iñaki Baz Castillo

4/7/2008 8:40:00 PM

0

El Lunes, 7 de Abril de 2008, Nobuyoshi Nakada escribi=F3:
> Hi,
>
> At Sun, 6 Apr 2008 22:54:52 +0900,
>
> I=F1aki Baz Castillo wrote in [ruby-talk:297234]:
> > Hi, is possible to define various methods in a single declaration?
> > something as:
> >
> > def method1, method2 (args)
> > ...
> > end
> >
> > Of course the above doesn't work, anyway to do it?
>
> Do you want alias?
>
> def method1(args)
> ...
> end
> alias method2 method1

I don't think aliases are valid for me since I want each method call "super=
"=20
(inheritance):

def debug(log_zone, msg) super(formatter(log_zone, msg)) end
def info(log_zone, msg) super(formatter(log_zone, msg)) end
def warn(log_zone, msg) super(formatter(log_zone, msg)) end
def error(log_zone, msg) super(formatter(log_zone, msg)) end
def fatal(log_zone, msg) super(formatter(log_zone, msg)) end
def unknown(log_zone, msg) super(formatter(log_zone, msg)) end

And I want:

def debug,info,war,error,fatal,unknown(log_zone, msg)
super(formatter(log_zone, msg))
end

Thanks a lot.


=2D-=20
I=F1aki Baz Castillo

Gennady Bystritsky

4/7/2008 9:18:00 PM

0

> -----Original Message-----
> From: I=F1aki Baz Castillo [mailto:ibc@aliax.net]
> Sent: Monday, April 07, 2008 1:40 PM
> To: ruby-talk ML
> Subject: Re: Is possible to define various methods at same time?
>
> El Lunes, 7 de Abril de 2008, Nobuyoshi Nakada escribi=F3:
> > Hi,
> >
> > At Sun, 6 Apr 2008 22:54:52 +0900,
> >
> > I=F1aki Baz Castillo wrote in [ruby-talk:297234]:
> > > Hi, is possible to define various methods in a single declaration?
> > > something as:
> > >
> > > def method1, method2 (args)
> > > ...
> > > end
> > >
> > > Of course the above doesn't work, anyway to do it?
> >
> > Do you want alias?
> >
> > def method1(args)
> > ...
> > end
> > alias method2 method1
>
> I don't think aliases are valid for me since I want each
> method call "super"
> (inheritance):
>
> def debug(log_zone, msg)
> super(formatter(log_zone, msg)) end
> def info(log_zone, msg)
> super(formatter(log_zone, msg)) end
> def warn(log_zone, msg)
> super(formatter(log_zone, msg)) end
> def error(log_zone, msg)
> super(formatter(log_zone, msg)) end
> def fatal(log_zone, msg)
> super(formatter(log_zone, msg)) end
> def unknown(log_zone, msg)
> super(formatter(log_zone, msg)) end
>
> And I want:
>
> def debug,info,war,error,fatal,unknown(log_zone, msg)
> super(formatter(log_zone, msg))
> end
>
> Thanks a lot.
>
>
> --
> I=F1aki Baz Castillo
>
>

How about something like this?

class CustomLogger
def initialize(*args)
@logger =3D Logger.new(*args)
end

def method_missing(name, *args)
case :debug, :info, :warn, :error, :fatal, :unknown
@logger.send name, formatter(*args)
else
@logger.send name, *args
end
end

def formatter(log_zone, msg)
# Do your formatting here
...
end
end

=3DGennady.

Gennady Bystritsky

4/7/2008 9:23:00 PM

0


>
> How about something like this?
>
> class CustomLogger
> def initialize(*args)
> @logger =3D Logger.new(*args)
> end
>
> def method_missing(name, *args)
> case :debug, :info, :warn, :error, :fatal, :unknown
> @logger.send name, formatter(*args)
> else
> @logger.send name, *args
> end
> end
>
> def formatter(log_zone, msg)
> # Do your formatting here
> ...
> end
> end
>
> =3DGennady.

Oops, correction in case usage:

class CustomLogger
def initialize(*args)
@logger =3D Logger.new(*args)
end

def method_missing(name, *args)
case name
when :debug, :info, :warn, :error, :fatal, :unknown
@logger.send name, formatter(*args)
else
@logger.send name, *args
end
end

def formatter(log_zone, msg)
# Do your formatting here
...
end
end