Hi -- I wasn't quite sure whether you wanted actual answers to your questions, so I decided you did :-) See below (or don't see, if you're still exploring the original and don't want to see commentary from me). On Sun, 16 Sep 2007, Kenneth LL wrote: > a = Array(11..20) > > class Foo > > attr_reader :a > > a = 123 > > def initialize(i) > @a = Array(1..10) > end > > def change() > puts > puts "Inside of change!!!!!!!!!!!!!!!!" > a[2] = 111111 # intentionally not using @a > > end > > def print_it > puts > p "Printing Object" > p @a > end > > end > > > > foo = Foo.new(3) > foo.print_it > > foo.change > foo.print_it > > foo.a[3] = 222222 > foo.print_it > > puts > p "Global var" > p a > > > ----------------------------------------------- > > if you like, you can write down the output of the above code, like in a > quiz... > > i will post the answer at the end of the post. > > but is this how to interpret the program? > > 1) you don't need to use @a[2] = 111111 in change() but can use a[2] = > 111111 because a is now the method name... it returns an array, and > therefore the instance method can modify the content of the array. > > 2) foo.a[3] = 222222 can actually modify the array like (1) above... so, > even though you are just using reader method, other routines can > actually modify the content of an object. the moral of the story is to > not return an array? because an array class is not protecting its > content -- any code can change its content. It's definitely a potential issue. You can protect against it by returning a dup of the array: class C attr_writer :a def a @a.dup end end There are some cases where you might want the object to be writeable, though, so you'd have to decide in each case. > 3) what is the "a = 123" near the line "class Foo" near the top of the > program? What is it, it is not local, not class variable, not instance > variable, not global... so what is it? the program is not complaining > the existence of it. It is local, actually. It's a local variable scoped to the outer level of the class definition body. It disappears inside method definitions, because "def" always starts a completely new local scope. > "Global var" > [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] That's actually also a local variable. You don't have any global variables in your program (they look like this: $a). It's a local variable at the top level of the program, which means it's not in the same scope as the class definition but is in scope (again) when that definition finishes. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)