1 Jul 2012 02:19
Re: Accessor Methods with a Twist
Jan E. <lists <at> ruby-forum.com>
2012-07-01 00:19:16 GMT
2012-07-01 00:19:16 GMT
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 " <at> " - class variables beginning with " <at> <at> " - 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(Continue reading)
RSS Feed