# Code quiz #1

**URL:** <https://www.ruby-forum.com/t/code-quiz-1/114583>\
**Category:** Ruby\
**Created:** [September 16, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/code-quiz-1/114583 "2007-09-16T13:45:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 16, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/code-quiz-1/114583/1 "2007-09-16T13:45:31Z")

</div>

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.

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.

NOW HERE IS THE OUTPUT OF THE PROGRAM ABOVE.  
MAKE SURE YOU DON’T WANT TO TRY AND WRITE DOWN THE ANSWER FIRST  
BEFORE LOOKING AT THE ANSWER:

* * *

C:\rails\depot\>ruby test\_class02.rb

“Printing Object”  
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Inside of change!!!

“Printing Object”  
[1, 2, 111111, 4, 5, 6, 7, 8, 9, 10]

“Printing Object”  
[1, 2, 111111, 222222, 5, 6, 7, 8, 9, 10]

“Global var”  
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 16, 2007, 3:13pm UTC](https://www.ruby-forum.com/t/code-quiz-1/114583/2 "2007-09-16T15:13:54Z")

</div>

On Sep 16, 5:13 am, removed\_email\_address@domain.invalid wrote:

> 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

can using just the reader but not the writer protect the data better?

> > 1. 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.

so is there much use of the local variable in the class level scope?  
can you even have code / loop doing things… but when does it get  
executed?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 16, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/code-quiz-1/114583/3 "2007-09-16T15:19:03Z")

</div>

> 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

I remember in C++, when an object is returned, it is always the dup…

while in Java, when an object is returned, it is always just the pointer  
to the object, so it is the same object… and change to it will affect  
the original.

and if it is in C++, if an array is returned, it is also just the  
pointer to the array… so the data is not protected from change.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/code-quiz-1/114583/4 "2007-09-17T13:23:34Z")

</div>

Hi –

On Sun, 16 Sep 2007, Summercool Summercool wrote:

> > end
> 
> can using just the reader but not the writer protect the data better?

It can protect the whole thing from being reassigned, but it can’t  
protect mutable objects. In other words, even without a writer method  
you can still do:

obj.name \<\< “append this to a string”

and if obj.name returns you the name string, then the name string will  
get changed.

> so is there much use of the local variable in the class level scope?  
> can you even have code / loop doing things… but when does it get  
> executed?

It’s executed as soon as Ruby sees it:

class C  
x = 1  
puts “Hello”  
puts x  
end

If you run that, you get the output right away. It’s not all that  
commonly used, I’d say.

David

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/code-quiz-1/114583/5 "2007-09-17T13:23:32Z")

</div>

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:

> ## end p “Printing Object” foo.change
> 
> therefore the instance method can modify the content of the array.
> 
> 1. 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.

> 1. 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
