From: Jeremy Date: 2003-03-27T10:50:44+09:00 Subject: Re: Conditionally make a method private? dblack@superlink.net wrote: > Hi -- > > On Wed, 26 Mar 2003, Jeremy wrote: > >> For now, I finally settled on this little bit of code: >> >> module Status_messenger >> class Status >> def Status.set(val) >> @@status = val >> end >> >> def Status.get >> @@status >> end >> end >> >> def status=(val) >> Status.set val >> end >> >> def status >> Status.get >> end >> end > > Don't forget that the class variable @@status will be shared by any > and all that have access to it. In the following example, note that > M1's view of status changes when M2 changes it: > > class M1 > include Status_messenger > Status.set(true) > puts Status.get # true > end > > class M2 > include Status_messenger > Status.set(false) > end > > class M1 > puts Status.get # false > end > > Assuming I'm right that this isn't what you want, you might be better > off with class instance variables: > > class Status > class << self; attr_accessor :status > end > > or something in that family. Actually, if I understand what you are saying, this is EXACTLY what I want. What I was trying to accomplish was having an attribute that wasn't quite global, but more accessable than just a normal class attribute. I do want M1's view of the status to change when I change it in M2. I think that the best way to think of it is like this: I have two classes (a database class, and a menu class) that are different enough that it doesn't warrant doing inheritance from a single superclass, but I need any instances of the two classes to communicate directly. I didn't really want to muck up global space with an attribute, because I don't want all things to be able to access it. But I also didn't want to do a lot of hardcoding in each class to make them "aware" of each other. I wanted this to kind of act like a community whiteboard. Also, I needed something that could possibly "do more", such as formatting, or automatic message clearing, or things like that. Using the above code was about the best that I could come up with for such a beast. As stated in the message to John Johnson, the reasoning for the original posting was to see if I could have some of them to act only as senders of messages (the databases), and some as readers of messages (the menus). I still would like to do something like that, but that will take a bit more thinking for me yet. I'm still fresh meat in the ruby community, only having been using it for about 3 weeks now. If I've described something that is already existing in the ruby language, of if you have any suggestions or pointers for what I've said, I'd be glad to hear them. Thanks for the response! Jeremy