Adding instance variables using module include

M

me

Hi there,

If I have a module and class like this:

module VisitMemory
def mark_visited(method)
@methods_visited << method.my_declaration_signature
end

def already_visited?(method)
@methods_visited.include?(method.my_declaration_signature)
end
end

module Guff
module JavaSource
class Class
include VisitMemory
end
end
end

How can i ensure that all instances of Guff::JavaSource::Class is
going to have the instance variable @methods_visited ?

So that this will work:

Guff::JavaSource::Class.new.already_visited?(something)

Thanks very much,
Mike.
 
M

me

FWIW, this is the best I could do:

module VisitMemory
def mark_visited(method)
methods_visited << method.my_declaration_signature
end

def already_visited?(method)
methods_visited.include?(method.my_declaration_signature)
end
def methods_visited
@methods_visited ||= []
end
end

module Guff
module JavaSource
class Class
include VisitMemory
end
end
end
 
R

Robert Klemme

2008/1/28 said:
Hi there,

If I have a module and class like this:

module VisitMemory
def mark_visited(method)
@methods_visited << method.my_declaration_signature
end

def already_visited?(method)
@methods_visited.include?(method.my_declaration_signature)
end
end

module Guff
module JavaSource
class Class
include VisitMemory
end
end
end

How can i ensure that all instances of Guff::JavaSource::Class is
going to have the instance variable @methods_visited ?

require 'set'

module VisitMemory
def mark_visited(method)
(@methods_visited ||= Set.new) << method.my_declaration_signature
end

def already_visited?(method)
@methods_visited and
@methods_visited.include?(method.my_declaration_signature)
end
end
So that this will work:

Guff::JavaSource::Class.new.already_visited?(something)

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top