Is possible to define various methods at same time?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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
 
P

Phillip Gawlowski

-----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.mozdev.org

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

Iñaki Baz Castillo

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
 
J

James Gray

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


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

Iñaki Baz Castillo

El Domingo, 6 de Abril de 2008, James Gray escribi=F3:
El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribi=F3:

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
 
R

Robert Klemme

2008/4/7 said:
El Domingo, 6 de Abril de 2008, James Gray escribi=F3:


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
 
N

Nobuyoshi Nakada

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

Iñaki Baz Castillo

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

Iñaki Baz Castillo

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
 
J

Julian Leviston

What you probably want is this:

class CustomLogger < Logger
%w(debug info warn error fatal unknown).each do |msg|
eval("def #{msg}(log_zone, msg)\n super(formatter(log_zone, msg))=20
\n end\n")
end
end

Juilan.


Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) =20
VIDEO #3 out NOW!
http://sensei.zenunit.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top