Problems with mixins

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

Hi –

On Tue, 11 Jul 2006, Russell F. wrote:

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 :slight_smile:

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

Hi –

On Tue, 11 Jul 2006, Russell F. wrote:

include Parser

def initialize ()

The empty parens are definitely not part of the prgram’s essentials
:slight_smile: (You weren’t doing them before. Is it something I said? :slight_smile:

puts lineno # prints 10
x = Inner.new
end

class Inner

def initializ

The ‘e’ on the end of initialize is, however, essential :slight_smile:

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

unknown wrote:

Hi –

It should. Try again :slight_smile:

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()

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 :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs