From: Doug Jolley Date: 2012-07-01T07:37:15+09:00 Subject: Re: Accessor Methods with a Twist Thanks, Jan, so much for that *VERY* thorough response. > I seems you haven't fully understood the concept of object orientation. I'm sure that's an understatement. However, I am wrestling with it. :-) I was aware of many of the things that you mentioned in your response. I think that my real issue turns on this statement in your response: > Of course, you can do this not only on the top level but also in modules > or anywhere else. But you have to create an instance of Person first. My understanding was that when I did: > person_1 = Person.new 'Doug' the left-hand-side was a local variable. After reading your response, I thought that maybe it isn't. Maybe it's something else that would be available in a module. I used the following code (which draws heavily on your example) to test this hypothesis: class Person def initialize name @name = name end def name() @name end def name=(new_name) @name = new_name end end person_1 = Person.new 'Doug' module MyModule def self.show_name() puts(person_1.name()) end end MyModule.show_name() => ./test2:23:in `show_name': undefined local variable or method `person_1' for MyModule:Module (NameError) So, I'm not exactly sure what you mean when you say: > Of course, you can do this not only on the top level but also in modules > or anywhere else. I cannot seem to access the accessor methods from within a module. That's what I'd like to be able to do. Thanks ever so much for hanging in there with me. I think I'm on the verge of a major breakthrough in understanding. Thanks again. > Some other things to note: > - You don't have to write getters and setters by hand. You can simply > call "attr_accessor" within the class body and supply the variable names > as Symbols: Understood. > - You don't have to use parantheses for method calls. While many people > still use them for clarity, it's common to omit them for basic methods > like "puts" and getter methods (this makes it look like you were > actually accessing the variables of the object rather than calling > methods). I understand that and within the context of simple things like puts, I agree. It's just that I am trying to force myself to remember to use parenthesis with methods so that when I go to review the code it's more clear which identifiers are variables and which are methods. > - I wouldn't use "::" when calling module or class methods. This is > allowed, but I'd rather stick with the normal dot and use the double > colon only for constants. That's a revelation. Thanks. > - When define class methods inside a class body, you should use "self" > as the receiver rather than write out the actual class name. This makes > the code much more flexible, because renaming the class won't break the > whole code. Good point. I'll try to remember that. ... doug -- Posted via http://www.ruby-forum.com/.