[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problems with mixins

Russell Fulton

7/10/2006 10:06:00 PM

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

4 Answers

dblack

7/10/2006 10:29:00 PM

0

Russell Fulton

7/11/2006 12:47:00 AM

0

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

dblack

7/11/2006 12:57:00 AM

0

Russell Fulton

7/11/2006 1:25:00 AM

0

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