Hi Folks,
I'm have some problems with mixin from a module that I have
written.
Module Parser
def thing
.
.
end
end
class Outer
include Parser
def initialize
print thing # works
end
class Inner
def initialize
print thing # fails with undefined local variable or method
`thing'
end
end # of Inner
end # of Outer
I've tried an include Parser in inner but that does not make any
difference
How can I access the mixins from embedded classes?
Cheers, Russell
···
--
Posted via http://www.ruby-forum.com/.
Hi --
Hi Folks,
I'm have some problems with mixin from a module that I have
written.
Module Parser
def thing
.
end
end
class Outer
include Parser
def initialize
print thing # works
end
class Inner
def initialize
print thing # fails with undefined local variable or method
`thing'
end
end # of Inner
end # of Outer
I've tried an include Parser in inner but that does not make any
difference
It should. Try again
module Parser
def thing
"Thing"
end
end
class Outer
include Parser
def initialize
puts thing
end
class Inner
include Parser
def initialize
puts thing
end
end
end
Outer::Inner.new # => Thing
David
···
On Tue, 11 Jul 2006, Russell Fulton wrote:
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS, the Ruby book for
Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me
unknown wrote:
Hi --
>
It should. Try again
Thanks David, I thought it should...
It would appear that the problem is related to how the control is passed
between the classes. I am instantiating Inner from Outer...
I have reduced my program to its essentials:
module Parser
def lineno
10
end
end
class Outer
include Parser
def initialize ()
puts lineno # prints 10
x = Inner.new
end
class Inner
def initializ
puts lineno # gives error
end
end
Outer.new()
···
--
Posted via http://www.ruby-forum.com/\.
Hi --
unknown wrote:
Hi --
It should. Try again
Thanks David, I thought it should...
It would appear that the problem is related to how the control is passed
between the classes. I am instantiating Inner from Outer...
I have reduced my program to its essentials:
module Parser
def lineno
10
end
end
class Outer
include Parser
def initialize ()
The empty parens are definitely *not* part of the prgram's essentials
(You weren't doing them before. Is it something I said?
puts lineno # prints 10
x = Inner.new
end
class Inner
def initializ
The 'e' on the end of initialize *is*, however, essential
puts lineno # gives error
You still haven't included Parser in Inner. If you don't, instance of
Inner won't have Parser's methods. (See my example again.)
end
end
You're missing an end here.
Outer.new()
What it comes down to is that module inclusion is per class. A nested
class is still a completely different class from the class it's nested
in, so you need to include the module in the inner class separately.
David
···
On Tue, 11 Jul 2006, Russell Fulton wrote:
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS, the Ruby book for
Rails developers
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me
Thanks again David!
What it comes down to is that module inclusion is per class. A nested
class is still a completely different class from the class it's nested
in, so you need to include the module in the inner class separately.
I swear that I tried putting includes in the inner modules at one stage,
sigh..
But I have been fiddling with various things and must have been holding
my mouth the wrong way at the time
After going back and adding 'include Parser' to all the inner modules I
am now back in business. Originally Parser was a class and I was
passing an instance of it to new for each inner class. I decided that
doing it with a mixin was a better approach since I will never have more
than one Parser at a time.
Russell
···
--
Posted via http://www.ruby-forum.com/\.