[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Adding instance variables using module include

me

1/28/2008 3:42:00 PM

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

me

1/29/2008 11:31:00 AM

0

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

Robert Klemme

1/29/2008 1:16:00 PM

0

2008/1/28, me@mikehogan.net <me@mikehogan.net>:
> 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

--
use.inject do |as, often| as.you_can - without end