What is scope?

. 1 min read

Beginners ask the hardest questions. Can you answer this week's question without using too much technical jargon?

What is scope?

This is a fantastically difficult question. I recommend allowing the student to understand scope themselves, through examples and play.

After some exercises, you might like to try and give a verbal explanation of scope. My favourite explanation is from Paolo Perotta's mindblowingly beautiful book, Metaprogramming Ruby.

Imagine being a little debugger making your way through a Ruby program. You jump from statement to statement until you finally hit a breakpoint. Now catch your breath and look around. See the scenery around you? That's your scope.

You can see bindings all over the scope. Look down at your feet, and you see a bunch of local variables. Raise your head, and you see that you're standing within an object, with its own methods and instance variables; that's the current object, also known as self. Farther away, you see the tree of constants so clear that you could mark your current position on a map. Squint your eyes, and you can even see a bunch of global variables off in the distance.

Photo by Robert Lukeman on Unsplash.