Hello and welcome to this lecture.
In this lecture or Our be talking to you about variable scope in Ruby.
What is a variable scope is scope basically describes the areas within a program where the variable
is assessable from.
There are several types of scopes available within Ruby.
We've caught what is known as a local variable.
You can usually tell the scope of a variable by their name.
So with a local school the name is usually in law case or in lower case.
But on score as well see you have to depend on the name have a variable that's called an amount on the
score order.
You can tell if you see a lower case you can tell that is the local variable or if it is just a variable
chord amount or order.
You can also tell as long as it's in lower case and it contains a character A to Z or an score.
You can identify that as the local variable local variable available in a local scope.
A little cool variable or lack of variables are local to the code construct in which they are declared.
For example if you have a function and you declare a variable within that function that variable is
only accessible from within that function.
That will be the scope.
We've also got a global scope of global variable and you can identify that here.
The name of the variable has got a dollar sign in front of it.
So global variables in Ruby are sensible from anywhere in the Ruby program.
This is regardless of where they are hard to click so it doesn't matter where you declare the variable
it is global.
It is a sensible anywhere within the Ruby program.
There are advantages disadvantage Surrey The Disadvantages of an arsehole is not encouraged.
Not a lot of people in quarries using global variables.
The reason being is that when you use global variables they are visible anywhere in the code or hip
program.
The code record for your program anywhere within that code you can see the global variables and they
can also be changed from anywhere within the application.
So this makes it very difficult to track any bug changes or any issues with the code makes it very difficult
to track the global variable.
If the scope is global.
Next we've got the instance variables.
You can identify that with the dollar sign then names usually have just at the last I'm sorry go.
The at symbol in front of the name.
So whereby you see the at symbol in front of a name it's an instance variable what is available within
the instance.
So basically an instance the values are local to specific instances of an object.
Okay so a b will only be available within a specific instance of an object.
Next we've also got what is known as a class scope.
You can identify the variables in a class score because you've got double at symbol a class called classes
in Ruby B's clean and first class objects so each class is an instance of a class of the ruby class.
If you know what I mean.
So with new classes created an object of the type class is in initialize had assigned a global constant
So when you create the class you are beat you don't expect the name of the class to change.
It becomes constant.
Then body it will have other things within that class.
But you can instantiate.
You can create an instance of that class or the instances of that class or objects that belong to that
class.
All share the same variable scope.
So a class variable is a variable that is shared among all the instances of a class with also got what
is called a constant.
Then you can identify a constant.
By the way they are named.
So with a constant you usually have it in capital capitals.
Or the first letter is usually capital.
You can identified because of capitals so Ruby constants are values which are which ones you've assigned
the values are not meant to be changed.
However Ruby does let you change them but it will warn you before you change them in all other programming
languages.
It doesn't you may not be able to do that once you've given it a value.
But Ruby does let you change it but give me a warning before it does.
Ruby also have what we called pseudo variables so this the variable variables which can not be assigned
values you can't give them values.
Example include Neil.
Neil basically is is a sign to initialize a variable so when you create a variable you don't give it
a value is referred to as nil.
Also you've got self serve basically refers to the currently executing object.
You can usually detect the scope of a variable by their names but you can also programmatically detect
what scope a variable in.
So what do we do that there is a method you can use.
So let's say I created a variable and then give you the value of 7.
Okay so we know that variable is 7.
So if I want to find out what scope that variable it's in.
Already know it's a local variable because it's in lower case.
Well I can also find that out by type name the method defined.
Followed by a question mark.
Space and then a variable name and press centre.
You can see it told me that is a local variable.
So if I did the same thing I created say I have a dollar sign X and said that two equals to seven.
If I come here and do find.
Question mark.
And then Darla X here to me this gorp is global.
So you know you get the idea.
So you can do the same once you know the symbol of how to identify.
You can usually identify by the by the symbols in front of variable names.
So you know that you can use that to identify or you can do programmatically I have done here.
So that's basically it.
How about variable scopes in Ruby.
Thanks for watching.
I hope it has made sense if it hasn't.
Please let me know.
I'll be more than happy to explain further.
Thanks for watching.
Thanks for your time.
Bye.