Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class and Mixin with same method name problem

Reply
Thread Tools

Class and Mixin with same method name problem

 
 
petermichaux@yahoo.com
Guest
Posts: n/a
 
      11-04-2005
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

 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      11-04-2005
On Friday 04 November 2005 17:32, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>
> =3D=3D=3D=3D CODE =3D=3D=3D
>
> module Debug
> def whoAmI?
> "#{self.class.name}"
> end
> end
>
> class EightTrack
> include Debug
> def whoAmI?
> Debug.whoAmI?
> end
> end
>
> et =3D EightTrack.new
>
> p et.whoAmI?
>
>
> =3D=3D=3D OUTPUT ERROR =3D=3D=3D
>
> peter$ ruby mixin.rb
> mixin.rb:10:in `whoAmI?': undefined method `whoAmI?' for
> Debug:Module (NoMethodError)
> from mixin.rb:16


Try this:
=2D---------------------
module Debug
=A0 def whoAmI?
=A0 =A0 "#{self.class.name}"
=A0 end
end

class EightTrack
=A0 include Debug
end

et =3D EightTrack.new

p et.whoAmI?
=2D---------------------

Regards,
Stefan


 
Reply With Quote
 
 
 
 
ts
Guest
Posts: n/a
 
      11-04-2005
>>>>> "p" == petermichaux <(E-Mail Removed)> writes:

p> class EightTrack
p> include Debug
p> def whoAmI?
p> Debug.whoAmI?

super

p> end
p> end


Guy Decoux


 
Reply With Quote
 
petermichaux@yahoo.com
Guest
Posts: n/a
 
      11-04-2005
ts, Thank you. Peter

 
Reply With Quote
 
petermichaux@yahoo.com
Guest
Posts: n/a
 
      11-04-2005
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?

 
Reply With Quote
 
Stefan Lang
Guest
Posts: n/a
 
      11-04-2005
On Friday 04 November 2005 18:07, (E-Mail Removed) 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?


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.
> def whoAmI?
> super
> end
> end
>
> et = EightTrack.new
>
> p et.whoAmI?


--
Stefan


 
Reply With Quote
 
petermichaux@yahoo.com
Guest
Posts: n/a
 
      11-04-2005
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?

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-04-2005
(E-Mail Removed) 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.

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?



 
Reply With Quote
 
petermichaux@yahoo.com
Guest
Posts: n/a
 
      11-04-2005

Robert Klemme wrote:
> (E-Mail Removed) 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.


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

http://www.ruby-doc.org/docs/Program...t_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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-04-2005
(E-Mail Removed) wrote:
> Robert Klemme wrote:
>> (E-Mail Removed) 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.

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

>
> Good to know. I just cut and pasted the example from the the first
> edition of Ruby Programming
>
> http://www.ruby-doc.org/docs/Program...t_modules.html


Indeed. Then that's a sub optimal example.

> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixin module with class variables and class methods John Lane Ruby 6 02-09-2010 09:48 PM
Mixin a module method as a class method Farrel Lifson Ruby 9 10-19-2006 12:06 PM
get mixin's name Payton Swick Ruby 5 12-29-2005 09:15 PM
MixIn method to call the method it overrides: how? Mac Python 1 06-18-2005 02:30 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments