Learn Ruby
Variables
5.4
Lesson

Variable scope

We have an array and we want to double each of the numbers. It can be done like this.

Let’s assume that someone by mistake puts that array2 inside the do end block as shown below.

If we run the above code we will get an error. Why?

We are getting error because the variable array2 is declared inside the do end block. A variable that is born inside a do end block is not visible outside of the do end block.

It means a variable has a scope. If we try to access a variable outside of its scope then we will get an error.

If a variable is born inside a do end block then that variable’s scope is limited to that do end block. Outside of that do end block no one knows about that variable.

So how do we fix the previous error. We need to take variable array2 outside of the do end block as shown below.