Is possible to define various methods at same time?

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.

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

Iñaki Baz C. 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 G.
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-----

El Domingo, 6 de Abril de 2008, Phillip G. 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

I will try what you suggest. Thanks.

El Domingo, 6 de Abril de 2008, James G.
escribió:> > end

end

c = Child.new
c.example1
c.example2

END

Hope that helps.

Sure, thanks :slight_smile:

On Apr 6, 2008, at 9:56 AM, Iñaki Baz C. wrote:

...

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 = Child.new
c.example1
c.example2

END

Hope that helps.

James Edward G. II

2008/4/7, Iñaki Baz C. [email protected]:

El Domingo, 6 de Abril de 2008, James G. escribió:

Hope that helps.

Sure, thanks :slight_smile:

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

Hi,

At Sun, 6 Apr 2008 22:54:52 +0900,
Iñaki Baz C. 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

El Lunes, 7 de Abril de 2008, Robert K.
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.

Thanks a lot for your help.

El Lunes, 7 de Abril de 2008, Nobuyoshi N.
escribió:> > 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ñaki Baz C. wrote in [ruby-talk:297234]:

super(formatter(log_zone, msg)) end

Iñaki Baz C.

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.

El Lunes, 7 de Abril de 2008, Gennady B.
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.

  @logger.send name, formatter(*args)

=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

method name then

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.

http://rubylearning.com/satishtalim/ruby_method_missing.html

Gennady.

2008/4/7, Iñaki Baz C. [email protected]:

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.

Kind regards

robert

El Lunes, 7 de Abril de 2008, Gennady B.
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.

http://rubylearning.com/satishtalim/ruby_method_missing.html

Ops, thanks, very interesting :wink:

2008/4/8, Robert K. [email protected]:

Thanks a lot. Finally it was not so important for me as I thought initially,
def unknown(log_zone, msg) super(formatter(log_zone, msg)) end

I agree to Gennady: delegation might be a better choice over inheritance here.

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

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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs