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.
···
--
Iñaki Baz Castillo
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.
--
Iñaki Baz Castillo
Iñaki Baz Castillo wrote:
Hi, is possible to define various methods in a single declaration?
something
as:
def method1, method2 (args)
...
endOf 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
Hi,
At Sun, 6 Apr 2008 22:54:52 +0900,
Iñaki Baz Castillo wrote in [ruby-talk:297234]:
Hi, is possible to define various methods in a single declaration? something
as:def method1, method2 (args)
...
endOf course the above doesn't work, anyway to do it?
Do you want alias?
def method1(args)
...
end
alias method2 method1
--
Nobu Nakada
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.
El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribió:
Have you looked at #method_missing, and it's relatives, like
#instance_variable_set?
--
Iñaki Baz Castillo
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.
El Lunes, 7 de Abril de 2008, Nobuyoshi Nakada escribió:
Hi,
At Sun, 6 Apr 2008 22:54:52 +0900,
Iñaki 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ñaki Baz Castillo
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 = Child.new
c.example1
c.example2
__END__
Hope that helps.
James Edward Gray II
On Apr 6, 2008, at 9:56 AM, Iñaki Baz Castillo wrote:
El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribió:
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
How about something like this?
class CustomLogger
def initialize(*args)
@logger = 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
=Gennady.
-----Original Message-----
From: Iñaki 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ó:
> Hi,
>
> At Sun, 6 Apr 2008 22:54:52 +0900,
>
> Iñaki 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 method1I 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)) endAnd I want:
def debug,info,war,error,fatal,unknown(log_zone, msg)
super(formatter(log_zone, msg))
endThanks a lot.
--
Iñaki Baz Castillo
How about something like this?
class CustomLogger
def initialize(*args)
@logger = Logger.new(*args)
enddef method_missing(name, *args)
case :debug, :info, :warn, :error, :fatal, :unknown
@logger.send name, formatter(*args)
else
@logger.send name, *args
end
enddef formatter(log_zone, msg)
# Do your formatting here
...
end
end=Gennady.
Oops, correction in case usage:
class CustomLogger
def initialize(*args)
@logger = 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
Sure, thanks ![]()
El Domingo, 6 de Abril de 2008, James Gray escribió:
On Apr 6, 2008, at 9:56 AM, Iñaki Baz Castillo wrote:
> El Domingo, 6 de Abril de 2008, Phillip Gawlowski escribió:
>> 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
> ...
> endHere's one way you might go about that:
class Parent
def example1
puts "In example 1."
enddef example2
puts "In example 2."
end
endclass Child < Parent
%w[1 2].each do |suffix|
define_method("example#{suffix}") do
puts "Forwarding to example #{suffix}..."
super
end
end
endc = Child.new
c.example1
c.example2__END__
Hope that helps.
--
Iñaki Baz Castillo
But is "method_missing" a especial keyword? If it's a normal method name then
I will neet to call:
objetc.method_missing
that I don't want, I need using:
debug(...), warn(...)...
Thanks.
El Lunes, 7 de Abril de 2008, Gennady Bystritsky escribió:
def method_missing(name, *args)
--
Iñaki Baz Castillo
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
2008/4/7, Iñaki Baz Castillo <ibc@aliax.net>:
El Domingo, 6 de Abril de 2008, James Gray escribió:
> Hope that helps.
Sure, thanks
--
use.inject do |as, often| as.you_can - without end
method_missing(), if present, is automatically invoked by Ruby on an attempt to call a method not explicitly defined for an object. So you will be able to write objetc.debug(...), objetc.warn(...), etc.
Gennady.
-----Original Message-----
From: Iñaki Baz Castillo [mailto:ibc@aliax.net]
Sent: Monday, April 07, 2008 2:41 PM
To: ruby-talk ML
Subject: Re: Is possible to define various methods at same
time? [Correction]El Lunes, 7 de Abril de 2008, Gennady Bystritsky escribió:
> def method_missing(name, *args)But is "method_missing" a especial keyword? If it's a normal
method name then
I will neet to call:
objetc.method_missing
that I don't want, I need using:
debug(...), warn(...)...Thanks.
--
Iñaki Baz Castillo
Thanks a lot. Finally it was not so important for me as I thought initially,
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
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
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.
El Lunes, 7 de Abril de 2008, Robert Klemme escribió:
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.
--
Iñaki Baz Castillo
Ops, thanks, very interesting ![]()
El Lunes, 7 de Abril de 2008, Gennady Bystritsky escribió:
method_missing(), if present, is automatically invoked by Ruby on an
attempt to call a method not explicitly defined for an object. So you will
be able to write objetc.debug(...), objetc.warn(...), etc.
--
Iñaki Baz Castillo
I agree to Gennady: delegation might be a better choice over inheritance here.
Kind regards
robert
2008/4/7, Iñaki Baz Castillo <ibc@aliax.net>:
El Lunes, 7 de Abril de 2008, Robert Klemme escribió:
> 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,
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
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)) endAs you can see, all the methods body is exactly the same. I expected there
could be a cool way to do something as:def debug,info,war,error,fatal,unknown(log_zone, msg)
super(formatter(log_zone, msg))
endjust it.
--
use.inject do |as, often| as.you_can - without end
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))\n end\n")
end
end
Juilan.
Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW!
http://sensei.zenunit.com/
On 08/04/2008, at 6:38 AM, Iñaki Baz Castillo wrote:
El Lunes, 7 de Abril de 2008, Robert Klemme escribió:
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,
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
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)) endAs you can see, all the methods body is exactly the same. I expected there
could be a cool way to do something as:def debug,info,war,error,fatal,unknown(log_zone, msg)
super(formatter(log_zone, msg))
endjust it.
Thanks a lot for your help.
--
Iñaki Baz Castillo
PS: I would guess that the original Logger class provides some means
to control formatting, for example a pluggable formatter. Why can't
you use that?
Kind regards
robert
2008/4/8, Robert Klemme <shortcutter@googlemail.com>:
2008/4/7, Iñaki Baz Castillo <ibc@aliax.net>:
> El Lunes, 7 de Abril de 2008, Robert Klemme escribió:
>
>
> > 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,
> 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
> 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
> 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.I agree to Gennady: delegation might be a better choice over inheritance here.
--
use.inject do |as, often| as.you_can - without end