From: "Jan E." Date: 2012-07-01T09:19:16+09:00 Subject: Re: Accessor Methods with a Twist Doug Jolley wrote in post #1066813: > 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: It *is* a local variable. There are four types of variables in Ruby: - local variables beginning with a lowercase letter or an underscore - instance variables beginning with a single "@" - class variables beginning with "@@" - global variables beginning with a "$" All four have completely different features and purposes. A local variable is a temporary variable for a specific context (mostly a method). It isn't visible to other contexts, which is why your code doesn't work. Instance variables are persistent variables bound to a specific object. They carry the properties of this object (like the name). You can only access it from the context of the object -- but of course you can pass the value to the outside world through getter methods. A class variable can be accessed from the class it was created by as well as every subclass and every instance of those classes. You can use it to store common properties (for example, you may have different database classes all sharing a single database connection). But most people avoid using class variables, because they're obviously hard to control. Global variables are accessible from any context. You should avoid them as well. I think one of the problems is that you are used to circumvent the limitations of local variables by using instance variables on the top level. This *does* work, but it's rather a hack and somewhat keeps you from designing meaningful classes. > I cannot seem to access the accessor methods from within a module. > That's what I'd like to be able to do. The outer local variable isn't visible for the method. You have to create the Person object *inside* the method. However, there may be a misconception about methods: You seem to think of methods as some kind of static, independend functionalities (like the mathematical functions in the Math module). But in fact they're the "abilities" of a certain object. So asking how to access the name methods from a module really makes no sense. They don't exist on their own. What you can do is create an instance of the class, which will then have those methods. If you actually want to have independend *functions* rather than methods, you should define them as singleton methods in a module (i. e. in the module body with "self" as the receiver). But then you're not supposed to use instance variables, because this would break the whole idea. Well, this all probably sounds rather abstract and complicated. If you write down some actual code, it may be easier for us to give concrete advice. -- Posted via http://www.ruby-forum.com/.