Ruby/Variables Datatypes Operators

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Variables

If you are new to programming all together, the term "variable" may be completely new to you. This is why, before discussing variables in Ruby, I believe it would be much more appropriate to define what a variable is first. Let's cite Wikipedia:

"A variable is a symbolic representation denoting a quantity or expression" Hum...sounds too complicated. Let me give you an example:

x=5

This piece of code give "x" a value of 5. Now, let's say we have this:

y=3
x=5
if (x==5) 
x=x+y
end

The code above tests if x equals to 5 first, and if it is, then x takes a new value: the former value of x, which is 5 and adds y's value to it.In the end, x will be equal to 8.

Variables are absolutely necessary in writing computer code. Most of the data would not be able to be modified without using variables. Different types of variables exist in different programming languages, some may or may not be implemented in others. For the purpose of this tutorial, we will continue to focus only on variables in Ruby programming language.

With variables you can control data objects referenced.

In Ruby you can have:

number = 5

or

name = "mark"

In the first case, Ruby will "know" that number actually means 5. Think of it as a sort of nickname. In the 2nd example, Ruby will know that name is actually called "mark"

There are 5 types of variables in Ruby: constants,locals, global, instance and class. Let's talk about what each of them does:

1) a constant is of course...constant

2) locals can only be accessed from a part of the program

3) globals are accessible globally to the program

4) instance variables are associated to an object

5) class variables belong to a class

Do not worry if you don't know all these terms, you will the more you read from the tutorial.

This is pretty much as far as we're going to go into the topic of variables for the moment. You should already know the basics of variables by now after reading the above. To differentiate between all the types above, Ruby uses special characters.

1) a constant begins with an uppercase letter and it should not be defined inside a method

2) a local must begin with a lowercase letter or the _ underscore sign

3) a global begins with the $ sign; an uninitialized global has the value of "nil" and also produces a warning

4) instances begin with the @ sign; an uninitialized instance has the value of "nil" and also produces a warning

5) a class variable begins with double @@ and have to be first initialized before being used in a method definition, otherwise you will get an error if you refer to it without initializing

Initializing a variable means giving it an initial value that will also determine the type of the variable. It can be something insignificant, such as 0 to simply serve the purpose of initialization. Variables can be modified later in the program.

Besides true variables, pseudo-variables also exist (true, false, nil, self, __FILE__ and __LINE__ denoting the current file, respectively the current line in the file). True and false are pretty self-explanatory, self should be also, and nil has most of the times the meaning of "null" or "empty" or "not defined".

They are called pseudo-variables because they don't behave like real variables would, instead they act more like constants.

These are reserved words in Ruby. You must not use them as names of variables or functions or anything else beyond the purpose for which they have been created.

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

As Ruby continues to improve as a programming language, other reserved words might be added in future releases.

[edit] Ruby Datatypes

Datatypes are the types of data Ruby accepts and understands. Numbers, strings, ranges for example are datatypes. We will discuss numbers and strings now and the rest in the following chapters.

Numbers and strings are the basics.

Numbers

There are 2 types of numbers in Ruby: integers and floats (or decimals)

There are 2 classes of integer numbers in order to distinguish between their size. So, numbers between -2^62 and 2^62 - 1 or -2^30 and 2^30 -1 belong to the class Fixnum and are stored internally in binary format.Numbers outside those ranges belong to the Bignum class. Generally, you will not be aware of this because Ruby will assign a number it's correct type automatically in the background.

In fact, if you want to verify this by yourself, open up a Ruby console or command prompter like I showed you in the chapter regarding installing and using Ruby and type these commands:

x=5
puts x.type

Ruby will output "Fixnum" as the result of the "type" and "puts" commands.

  1. As of Ruby 1.8.6 you will get an error like:

warning: Object#type is deprecated; use Object#class \n Fixnum So just use

x=5
puts x.class
Next
Personal tools