On Sun, Jun 10, 2012 at 4:15 AM, Doug Jolley <[email protected]> wrote:
>> From the description I'm guessing you want to use the module as a mixin
>> to add methods to the class. In this case you don't even need the
>> accessor methods. If you include the module in the class, the methods of
>> the module have direct access to the instance variables -- just like
>> they were defined in the class.
>
> I don't think that I want to use a mixin. My understanding of a mixin
> is that it would cause me to loose the name space separation that I was
> seeking. Using a mixin would be pretty much identical to using the
> inheritance that you also suggested. What I sorely need is a block that
> would allow me to access the accessor methods that are outside the block
> but would otherwise enforce its own namespace. I'm going to have to
> reflect on this one.
Please do. From what I have read so far in the thread it is totally
unclear to me what you are trying to achieve. It seems to me that
this may be the case for you as well. :-)
Since accessors are public you can access them from anywhere - but you
need an instance to invoke them on. In your example this will work:
module MyModule
def MyModule.test1(obj)
puts(obj.fname())
end
end
MyModule.test1(my_instance)
I am suspecting though that you might not have made your mind up
completely on where the _state_ should reside. In your original
example you create a single instance and want to make that accessible
from a global function ("global" in the sense that it is not
associated with a particular instance of a class although it is of
course associated with the module instance).
If you want to do that then you need to store the instance somewhere
presumably as instance variable of the module:
module MyModule
@my_instance=MyClass.new()
def MyModule.test1()
puts(@my_instance.fname())
end
end
An alternative would be to make MyClass a singleton but that would
just change the place where you store the state.
Can you give a more realistic example of what you want to achieve?
Please explain what the role of MyClass and MyModule is supposed to be
in your application. That would help us tremendously to understand
what you are trying to achieve and come up with better feedback.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en