From: Robert Klemme Date: 2004-12-15T19:37:16+09:00 Subject: Re: Mixin questions "Johan Nilsson" schrieb im Newsbeitrag news:1103100242.441c484f346652f8ca3ea31133c45618@teranews... > > "Robert Klemme" wrote in message > news:328e5nF3jvaf9U1@individual.net... > > > > "Johan Nilsson" schrieb im Newsbeitrag > > news:1103032112.3aa9129fd718fc17e4d9eb2e027a3e52@teranews... > > > > > > "Robert Klemme" wrote in message > > > news:3286taF3it29pU1@individual.net... > > > > > > > > > [snip] > > > > > > > I still don't see how the mixin can get to the protected methods > > though - I > > > get "NoMethodError: protected method 'my_stuff' called for ..." if I try > > > somehing like my original code. > > > > I haven't a solution for this yet. I tried with instance_eval but to no > > avail. Maybe send works. Did you try that? > > No, and it actually works. What's the difference? I'm just starting to use > Ruby for real. #send bypasses a lot of things - that's why, I guess. > > > > > In english, I want to create an instance of Foo from within the > > mixin's > > > > > supplied class method. The first question still applies though. > > > > > > > > You mean like this? > > > > > > > > module Mix > > > > def test_it > > > > p self::FOO > > > > end > > > > end > > > > > > > > class Test > > > > extend Mix > > > > FOO = "bar" > > > > end > > > > > > Yes, but in my (unfortunately) more complex case I get "dynamic constant > > > assignment" whenever I add "self::" to the mixin (reported > > from > > > the point where the class constant is defined). > > > > But then you're assigning the constant more than once. From your example > > I though you just wanted to access (= read) it. Which Ruby version are > > you using? > > Sorry for being unclear, but I'm not assigning more than once (at least I'm > not trying to). I'm trying to use either self:: or > self.class.const_get(:) as rvalues, and the former does not work > in the Mixin. Oh, yes, and it is within a define_method scope - could that > matter, e.g.: I think it does. self at the time of execution is an instance and not the class object at hand. > def ... > define_method(:initialize) { || > ... > @x = self:: # dynamic constant assignment > ... > } > end So here are two alternatives: def ... define_method(:initialize) { || ... @x = self.class:: # dynamic constant assignment ... } end def ... me = self define_method(:initialize) { || ... @x = me:: # dynamic constant assignment ... } end > Platform is Ruby 1.8.2.rc9 on WinXP. > > [...some minute later...] Oh, but wait - self refers to the object here. > Trying to use self.class::ID works! Sorry for the noise. > > > > > > Using > > > ...self.class.const_get("") works fine though in the mixin. > > > > Strange. > > > > > The code is a bit too much to post though, if I can reduce it I'll try > > and > > > post the actual failing code instead. > > > > Yeah, it would be good to see the real source of the problem. At the > > moment I feel a bit tapping in the dark. :-) > > That might not be necessary any more, thanks! You're welcome. I'm glad I could help. Kind regards robert