Hi all,
Another question for the Ruby-omniscient ruby-talk ML folk! Joy!!
Anyway, here's one a little more difficult than my last one (I think,
anyway): is there a way for me to create methods in a class that can
only be called by an object of another class? (not even itself or other
objects of it's class)
What I am trying to do is this: imagine that the SimpleObject class had
two methods, called _inc and _dec. Now imagine that no instance on
SimpleObject or any of it's descendents could fire those methods. Nor
could any other living object in the current running program (or any)
fire these methods, EXCEPT a singleton instance of another class, called
Container, in which the instances of SimpleObject happen to be
"contained". (pun intended; they are just housed in a hash inside Container)
Here's an outline:
<dream_snippet>
class Container
include Singleton
def inc(obj)
if obj.instance_methods.include?('_inc') && \
obj.kind_of?('SimpleObject')
obj._inc # increment this object's ref count
end
end #inc
#-------problem area-----------
def caller_valid?(*args) # who knows what this method should take?
# make sure that the Container instance is the calling object
# or return false
end #caller_valid?
#------------------------------
end #Container
c = Container.instance
class SimpleObject
def initialize
@refCount = 0
end #initialize
attr_reader :refCount
def _inc
refCount += 1 if c.caller_valid?(<<???>>)
end #_inc
def _dec
refCount -= 1 if c.caller_valid?(<<???>>)
end #_dec
end #SimpleObject
x = SimpleObject.new
x._inc
x.refCount # => 0
c.inc(x)
x.refCount # => 1
</dream_snippet>
Any chance of doing this? If not, anyone know of a workaround? Thanks in
advance!!
--
0100001101000010010000110100011101110101
<< Tobias DiPasquale >>
-=[ Solaris Systems Administrator ]=-
Villanova University ECE Dept.
mailto: anany / ece.vill.edu
-=[ Applications Engineering Consultant ]=-
BCS Solutions, Inc.
mailto: tdipasquale / bcssinc.com
0100001101000010010000110100011101110101