Advice for Ruby Programming Foundations Written Assessment(Part 2)
Technical part of the assessment
Learn to use programming terms
Let’s consider this code :
How would you describe it. a
is hello
, b
is a
, then a
is goodbye
? Of course not…let’s break it down.
On line 1
of this code, local variable a
is initialized. This is the first, very important term that you will use many many times on the test. First time you assign certain value to some local variable you are initializing it. On line 1
you are initializing local variable a
and on line 2
you are initializing local variable b
.
Which brings me to the second term. On line 1
you are assigning the string object with a value hello
to the local variable a
(remember that objects are just physical spaces in memory and local variables are referencing them), so now, and here is our third word, local variable a
is referencing that string object with the value name.
So, to repeat, we are initializing a local variable a
to a string object, and now this local variable references this string object.
Now let’s talk about reassignment. On line 3
of this code, we are not initializing the local variable a, it is already initialized on line 1
, we are actually reassigning local variable a
so now it references a different string object with a different value goodbye
.
Let’s explain these 3 lines of code with markdown and paragraphs.
On line 1
of this code we are initializing the local variable a
to a string object with value hello
to it.
On line 2
we are initializing the local variable b
to a string object that the local variable a
is referencing. Currently, both of these local variables are pointing to the same object.
On line 3
we are reassigning the local variable a
to a different string object with value goodbye
so now, the local variable a
is pointing to one string object with value goodbye
and the local variable b
is pointing to a string object with value hello
.
On to a different example, and a few more terms…
This example is “a bit” longer. Let’s break it down line by line.
On lines 1–8
we are defining the method example
which takes 1 parameter. On line 10
we are calling the method example
and passing in the string hello
as an argument to it. And here is the first thing which confuses a lot of people. and it’s important to distinguish…Methods are defined with parameters, but they are called with arguments.
On line 2
of this code we are initializing the local variable i
and assigning to it an integer with value 3
.
On line 3
we are calling the method loop
(yes, loop is actually a method of the Kernel
module) and passing in the do..end
block as an argument. So the block here is is passed to a method call as an argument.
And also on line 4
we are actually calling the method puts
and passing in local variable str
to it as an argument. You are going to use a lot of passing in as an argument on the test. Get used to that phrase.
On line 5
the local variable i
is reassigned. -=
is actually reassignment and it is Ruby’s syntactical sugar for i = i — 1
. And while we are talking about syntactical sugars, that code is also one, since —
is not an operator but a method and that code can also be written as i = i.-(1)
. So inside of this code we are actually, reassigning the local variable i
to the return value of a method call Integer#-
on a local variable i
with integer 1
passed to it as an argument.
On line 6
we are breaking out of the loop by using thekeyword break
if the value of the object that local variable i
is referencing is equal to 0.
On line 10
we are calling the method example
and passing in string hello
as an argument.
Finally, but not less important, this code outputs string hello
3 times and returns nil. That is very important to distinguish. The last evaluated expression is returned since we don’t have an explicit return
inside of the method definition. That last evaluated expression is break if i == 0
in this case, which returns nil
.
Variable Scoping
Without running the code try to answer what will this code example output :
Finished? You haven’t run the code right? I will break down the code now and explain the output.
Result → The code outputs 5
and raises an error, undefined local variable or method b
.
Why is that? Haven’t we initialized the local variable b
on line 5
? Well, we have, but here we have two scopes. An inner scope which is defined by the do..end
block and outer scope which includes everything else. Now comes the important part -> local variables that are initialized in an inner scope CAN’T be accessed in the outer scope, but local variables that are initialized in the outer scope CAN be accessed in an inner scope.
This is why we can reassign the local variable a
on line 4, but if we try to initialize the variable b
it is not accessible in the outer scope. Can you find a way to make this code output the value of the local variable b
? I will give you the answer below :
On line 2
(in the outer scope) we have initialized the local variable b
so now it is available in the inner scope defined by the block and when we call puts
method on line 11 it outputs 3
(since the variable was reassigned on line 6
) and returns nil
(since puts
method always returns nil).
Without running the code try to answer what will this code example output :
Result → This codes outputs 3
and 2
.
I guess output 2
is self explanatory so I won’t go into it in much detail, but let’s break down what happens with the local variable a
.
On line 1
we are initializing a local variable a
with in outer scope.
On line 4
we call theloop
method and pass thedo..end
block to it as an argument. Inside of this block we initialize new local variable c
which has this block as its scope.
On line 6
we reassign the local variable a
so that it points now to the same object that the local variable c
is referencing. Since we are still in the inner scope defined by the block, local variable c
is accessible.
On line 10
we call theputs
method and pass the local variable a
as an argument and this code outputs thevalue of variable a
which is 3
now since we reassigned it inside of the block.
What would happen if on line 12
we added puts c
? It would return an error of course just like in the previous example, since this variable is not available in the outer scope.