Code quiz #1

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]

On Sep 16, 5:13 am, [email protected] 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?

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.

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

Hi –

I wasn’t quite sure whether you wanted actual answers to your
questions, so I decided you did :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs