Class and Mixin with same method name problem

Hi,

I'm trying to call a modual method from a class method with the same
name. The code I tried and error are below. I think my "Debug.whoAmI?"
line is the problem. What is the correct syntax to call the moduals
method?

Thanks,
Peter

==== CODE ===

module Debug
  def whoAmI?
    "#{self.class.name}"
  end
end

class EightTrack
  include Debug
  def whoAmI?
    Debug.whoAmI?
  end
end

et = EightTrack.new

p et.whoAmI?

=== OUTPUT ERROR ===

peter$ ruby mixin.rb
mixin.rb:10:in `whoAmI?': undefined method `whoAmI?' for Debug:Module
(NoMethodError)
        from mixin.rb:16

Try this:

···

On Friday 04 November 2005 17:32, petermichaux@yahoo.com wrote:

Hi,

I'm trying to call a modual method from a class method with the
same name. The code I tried and error are below. I think my
"Debug.whoAmI?" line is the problem. What is the correct syntax to
call the moduals method?

Thanks,
Peter

==== CODE ===

module Debug
  def whoAmI?
    "#{self.class.name}"
  end
end

class EightTrack
  include Debug
  def whoAmI?
    Debug.whoAmI?
  end
end

et = EightTrack.new

p et.whoAmI?

=== OUTPUT ERROR ===

peter$ ruby mixin.rb
mixin.rb:10:in `whoAmI?': undefined method `whoAmI?' for
Debug:Module (NoMethodError)
        from mixin.rb:16

----------------------
module Debug
def whoAmI?
"#{self.class.name}"
end
end

class EightTrack
include Debug
end

et = EightTrack.new

p et.whoAmI?
----------------------

Regards,
  Stefan

class EightTrack
  include Debug
  def whoAmI?
    Debug.whoAmI?

       super

  end
end

Guy Decoux

ts, Thank you. Peter

What if two moduals are included and each uses the same method name? In
the example below the Debug whoAmI? method is called. How do I call the
Burp whoAmI? method?

Thanks,
Peter

module Debug
  def whoAmI?
    "#{self.class.name}"
  end
end

module Burp
  def whoAmI?
    "Burp #{self.class.name}"
  end
end

class EightTrack
  include Burp
  include Debug
  def whoAmI?
    super
  end
end

et = EightTrack.new

p et.whoAmI?

What if two moduals are included and each uses the same method
name? In the example below the Debug whoAmI? method is called. How
do I call the Burp whoAmI? method?

There is no clean and simple way to do so.

module Debug
  def whoAmI?
    "#{self.class.name}"
  end
end

module Burp
  def whoAmI?
    "Burp #{self.class.name}"
  end
end

class EightTrack
  include Burp
  include Debug

The following method definition is useless.
Remove it and you'll get the same result.

···

On Friday 04 November 2005 18:07, petermichaux@yahoo.com wrote:

  def whoAmI?
    super
  end
end

et = EightTrack.new

p et.whoAmI?

--
Stefan

petermichaux@yahoo.com wrote:

What if two moduals are included and each uses the same method name?
In the example below the Debug whoAmI? method is called. How do I
call the Burp whoAmI? method?

You don't (although you could with Burp.instance_method(:whoAmI?).bind(self).call). Messing like this with names is not advisable.

Note also that "self.class.name" will always yield the same result regardless in which module the method was defined. Note also that by convention a) methods ending in a question mark are reserved for boolean queries and b) Ruby uses lowe_case_method_names. :slight_smile:

Kind regards

    robert

···

Thanks,
Peter

module Debug
def whoAmI?
   "#{self.class.name}"
end
end

module Burp
def whoAmI?
   "Burp #{self.class.name}"
end
end

class EightTrack
include Burp
include Debug
def whoAmI?
   super
end
end

et = EightTrack.new

p et.whoAmI?

no clean and simple way? bummer. I thought there would be some sort of
namespace separation between the two moduals. When they are mixed into
the EightTrack class are the namespaces lost?

Robert Klemme wrote:

> How do I call the Burp whoAmI? method?

You don't (although you could with
Burp.instance_method(:whoAmI?).bind(self).call). Messing like this with names is not advisable.

Good to know that there is a way to do this. Too bad it is so messy
though. I'm thinking about using two Rails plugins in one model class.
Since rails has a magic after_find callback method I was thinking about
using this in each plugin since the plugins can be used independently.
Since my model must call both of these methods I will have a third
after_find callback method in the model that calls the other two. Now
why doesn't the example below make two who_am_i calls? One to Burp and
one to Debug. It only calls Burp.

Note also that by
convention a) methods ending in a question mark are reserved for boolean
queries and b) Ruby uses lowe_case_method_names. :slight_smile:

Good to know. I just cut and pasted the example from the the first
edition of Ruby Programming

http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html

Peter

module Debug
  def who_am_i
    "Debug"
  end
end

module Burp
  def who_am_i
    "Burp"
  end
end

class EightTrack
  include Burp
  include Debug
  def who_am_i
    Debug.instance_method(:who_am_i).bind(self).call
    Burp.instance_method(:who_am_i).bind(self).call
  end
end

et = EightTrack.new

p et.who_am_i

···

petermichaux@yahoo.com wrote:

petermichaux@yahoo.com wrote:

Robert Klemme wrote:

How do I call the Burp whoAmI? method?

You don't (although you could with
Burp.instance_method(:whoAmI?).bind(self).call). Messing like this
with names is not advisable.

Good to know that there is a way to do this. Too bad it is so messy
though. I'm thinking about using two Rails plugins in one model class.
Since rails has a magic after_find callback method I was thinking
about using this in each plugin since the plugins can be used
independently. Since my model must call both of these methods I will
have a third after_find callback method in the model that calls the
other two.

I don't know Rails so I can't comment on this. I just would assume that DHH would have thought of this scenario. At least it sounds reasonable. Or you're abusing these plugins.

Now why doesn't the example below make two who_am_i calls?
One to Burp and one to Debug. It only calls Burp.

Wrong. Look again. :slight_smile:

Note also that by
convention a) methods ending in a question mark are reserved for
boolean
queries and b) Ruby uses lowe_case_method_names. :slight_smile:

Good to know. I just cut and pasted the example from the the first
edition of Ruby Programming

Programming Ruby: The Pragmatic Programmer's Guide

Indeed. Then that's a sub optimal example. :slight_smile:

Peter

module Debug
def who_am_i
   "Debug"
end
end

module Burp
def who_am_i
   "Burp"
end
end

class EightTrack
include Burp
include Debug
def who_am_i
   Debug.instance_method(:who_am_i).bind(self).call
   Burp.instance_method(:who_am_i).bind(self).call
end
end

et = EightTrack.new

p et.who_am_i

Kind regards

    robert

···

petermichaux@yahoo.com wrote:

RK wrote:

petermichaux wrote:
> Robert Klemme wrote:

> Now why doesn't the example below make two who_am_i calls?
> One to Burp and one to Debug. It only calls Burp.

Wrong. Look again. :slight_smile:

Peter, you've annoyed Robert - he won't help you now :wink:

If you go to the trouble of calling a method, you may as well
do something with its return value rather than just losing it.

module Debug
  def who_am_i
    'Debug'
  end
end

module Burp
  def who_am_i
    'Burp'
  end
end

module Throb
# #def who_am_i
# # 'Throb'
# #end
end

class EightTrack
  include Burp
  include Throb
  include Debug
  def who_am_i
    wai = [self.class]
    ( self.class.ancestors - [Object, Kernel] - wai ).each do |a|
      ( wai << a.instance_method(:who_am_i).bind(self).call ) rescue nil
    end
    wai
  end
end

et = EightTrack.new

p et.who_am_i # [EightTrack, "Debug", "Burp"]

daz

Robert Klemme wrote:

> Now why doesn't the example below make two who_am_i calls?
> One to Burp and one to Debug. It only calls Burp.

Wrong. Look again. :slight_smile:

Victim of a new language. I got it. Thanks!

Peter

···

petermichaux@yahoo.com wrote:

> module Debug
> def who_am_i
> "Debug"
> end
> end
>
> module Burp
> def who_am_i
> "Burp"
> end
> end
>
> class EightTrack
> include Burp
> include Debug
> def who_am_i
> Debug.instance_method(:who_am_i).bind(self).call
> Burp.instance_method(:who_am_i).bind(self).call
> end
> end
>
> et = EightTrack.new
>
> p et.who_am_i

daz wrote:

If you go to the trouble of calling a method, you may as well
do something with its return value rather than just losing it.

Actually Robert's cryptic smiley lead me to this. But thanks for the
straight goods.

Peter

daz wrote:

RK wrote:

petermichaux wrote:

Robert Klemme wrote:

Now why doesn't the example below make two who_am_i calls?
One to Burp and one to Debug. It only calls Burp.

Wrong. Look again. :slight_smile:

Peter, you've annoyed Robert - he won't help you now :wink:

LOL. No, not really. (Now you want to know where that "no" refers to...
:-)))

Kind regards

    robert