Ruby, the scripting language used by Adhearsion, is a fully OO language, which means that EVERYTHING is an object. This is something to keep in mind while progressing through this primer, and while developing Adhearsion applications.
This primer is not intended as an all-encompassing tutorial on Ruby, it is simply a basic guide to get you started with Adhearsion, and help you understand the example code.
"Variables"
In traditional programming, variables are essentially names that refer to a piece of information, such as a number or some text. Since Ruby is OO, variables refer to instances of classes.
For example:
results in somevar referring to an instance of the Integer (aka whole numbers) class. Ruby creates this instance on the fly for you. The "5" is what is called a literal. Generally speaking, literals only exist for basic data types such as Strings (text) and Integers.
Variables can also be set to equal other variables:
somevar = 5
anothervar = somevar
results in anothervar have a value of 5. It is important to realize that this assignment actually creates a copy of the Integer object referenced by somevar, instead of acting as a pointer to it.
With the above in mind, let's take a look at string variables:
somevar = "hello"
anothervar = world
the above code will result in somevar referring to an instance of the String class, and having a value "hello". I refer to this value in quotes for ease of understanding, but the string will only have the text inside the quotes. On the other hand, anothervar will contain nothing! Indeed, you will get an error if you tried that line without setting world to something. This is because world, sans quotes, is interpreted as a variable name, and not a string.
Choosing names for variables isn't really difficult, but there are some things to watch out for:
- really short variable names just create confusion. "r12" is not a good variable name
- really long variable names take a long time to type
- be careful when using "generic" variable names. Adhearsion creates quite a few helpful objects for you to use, the downside being that you can't use these objects' names. Instead of "number" use "phonenumber", etc.
Flow Control
Comments (0)
You don't have permission to comment on this page.