Jump to content
xisto Community
Sign in to follow this  
DeM0nFiRe

The Basics Of Object Oriented Programming [lesson 2] Introduction to Variables

Recommended Posts

Hello everyone, and welcome to

 

The Basics of Object Oriented Programming

[Lesson 2] By DeM0nFiRe

 

Lesson 1 can be found Here. I really recommend you read that first before you look here.

 

Lesson 2 starts to get more complicated, so you may want to take notes :P I'm not going to be babying you guys anymore, and we're going to use actual terms to describe what is going on (Don't worry, I'll define a term the first time I use it :P ) If you don't already have some understanding of program, this will require your full attention. If you are listening to music right now, I recommend you turn it off before you start this one :P There's a reason that variables are getting their own tutorial.

 

What are "variables"?

*Note: Even if you think you know what variables are, I would recommend that you take a look at this anyway*

If you have ever taken Algebra or a similar mathematics course, then variables to you would probably mean some letter that stands for some number that will change (get it: vary <--> variable), which is actually pretty close to what a variable is. If you have ever taken a basic Programming class or read a basic tutorial, then variables to you would probably mean something that holds a number or some other type of data, which is not what a variable is at all. *It is important to understand this concept right now: your computer's memory holds the information and the variables just point to the information* In actuality, a variable is just an Abstract Concept that programmers use to talk about something that doesn't really exist. Now, this is a recurring theme in programming, that programmers will use words to talk about something that doesn't actually exist. This is because it is a lot easier to think about programming this way. Now, in the future I will be talking about Variables in the same way, however first I want you guys to understand what a variable really is.

 

A variable in general consists of two things: and Identifier and a Reference.

The identifier part of a variable is what you call the variable, the name of the variable. Sounds simple, right? Good, because it really is simple. Identifier can be a pretty big word, but it just means name. Now, there are rules to identifiers. For instance, you can't name your variable "3blindmice" because it starts with a number, and you can't name a variable "hello world" because there is a space. We'll go a little more in depth as to what the rules are a little further down, as the rules for identifiers vary slightly from language to language.

The Reference part of the variable is a bit more complicated. The Reference part of the variable is the part that points to (or references) the spot in your memory that holds your information. Sounds simple, right? Well, unfortunately this part is not that simple. The way these references work is very different from language to language. Unfortunately, beyond this I can only tell you what references do specifically for Ruby.

 

A variable in Ruby works as follows:

 

Identifiers in Ruby have several rules that you must follow:

-Identifiers can contain only alphanumeric (a-z, A-Z, and 0-9) and the underscore ( _ )

-Identifiers can never start with a number

-Identifiers can start only with a letter or underscore

-Identifiers cannot contain spaces

-Identifiers cannot contain any Keywords. A keyword is a word reserved by the language for doing certain things. The keywords in Ruby are as follows:

alias   and	 BEGIN   begin   break   case	class   def	 defined do	  else	elsif   END	 end	 ensure  false   for	 if in	  module  next	nil	 not	 or	  redo	rescue  retry return  self	super   then	true	undef   unless  until   when while   yield

*Note:This part gets very complicated, so pay very close attention*

*Note:Ruby references work very different from most languages, so if you want to learn a new language later make sure you research references for that language*

 

So, we talked about objects a small amount in the last tutorial. As I said, objects in programming are very similar to objects in the real world. Now, you wouldn't consider a number an object, would you? Well, most programming languages don't either HOWEVER in Ruby every single piece of data you will work with, including numbers, are objects. This means that everything will be Passed by Reference. What, exactly, does this mean? Well, let's take a look at some code!

 

our_object = Object.newour_other_object = our_object

So, what will this do? Well, let's take a look at a picture of our memory before we start:

 

--------------|			 ||			 ||			 |--------------|			 ||			 ||			 |--------------|			 ||			 ||			 |--------------

ahh, nice and empty, right?

 

so, after we run our first line we have:

 

--------------|			 | <--- our_object|  Object	 ||			 |--------------|			 ||			 ||			 |--------------|			 ||			 ||			 |--------------

As you can see, our_object is referencing the spot in the memory where we put the Object. So, now Passed By Reference means that when you set one variable equal to another, rather than the value being given to the variable's reference, the identifier of the variable gets a reference pointing to the same spot as the other variable. So, the second line of code will give us this:

 

--------------|			 | <--- our_object|  Object	 ||			 | <--- our_other_object--------------|			 ||			 ||			 |--------------|			 ||			 ||			 |--------------

So, as you can see, using = on a variable does not change the contents of the variable! Instead, it "assigns" the reference to point to an object in the memory. Assigning our_other_object to our_object didn't change any objects, all it did was make it so that our two variable pointed to the same location. So, let's look at some more examples:

 

a = 5b = 4a = b

So, after this code is run, how many spots in the memory are used? Just one. The first line will create an object that is the 5 and assign a to point to it. The next line creates an object that is a 4 and assigns b to point to it. The last line assigns a to point to the *same exact* 4 that b is pointing to. On this line, something else will happen as well. Don't you wonder what will happen to our 5 that we made with the first line? Well, in Ruby (and in some other languages, but not all!) there is a handy little thing called the Garbage Collector. The Garbage Collector will see that there are no longer any references pointing to that 5 and will remove it from the memory, freeing up that slot for future use.

 

So, this is a lot of information, so I will end this tutorial here. If you have any questions, please do not hesitate to ask! I know some of this can be confusing, so I'll try to answer any questions you have.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.