To Documents
Constants and Variables
 
Constants
Ruby has four types of constants: integer, floating point, string,
and boolean.
- Integer
- Ruby integers are positive, negative, or zero numbers
that do not have a fractional part, for example,
34,
643938,
-21,
-38472874,
or 0.
- Ruby integers are objects of the FixNum or BigNum
classes.
- FixNum integers have an approximate range from -2 billion to
2 billion.
- BigNum integers have a unlimited values.
- The price you pay for BigNum integers is speed; the larger they are,
the slower processing them gets.
- Floating Point
- Ruby floating point constants are positive or
negative numbers with a fractional part, for example,
46.1982,
0.00441,
-237.1273. The constant
23.000 also qualifies as a floating
point number, even though it could be converted to an integer without loss of precision.
Floating point numbers can also be written in base 10 scientific notation with positive
or negative exponents. Here are some examples:
Ruby Constant | Scientific Notation | Meaning |
3.0e2 |
3.0 x 102
| 300 |
5.98e24 |
5.98 x 1024
| Mass of Earth in Kilos |
9.11e-31 |
9.11 x 10-31
| Mass of Electron in Kilos |
- Ruby double precision floating point numbers are objects of the
Float class.
- Always include a digit between the decimal point and the exponent.
Do this: 3.0e15, not 3.e15.
- The range of Float numbers is machine dependent,
but is usually at least -1.0 × 10300 to
1.0 × 10300.
- Float numbers are usually double precision, having at about 15 or
16 significant digits.
- String
 
- Boolean
 
- A boolean constant can take on either of the values
true or
false.
- false can also be represented by nil (undefined value). Any other
value, including values from FixNum, BigNum, String, or
Float is interpreted as true.
- true is also represented by anything else (other than false or nil).
Variables
A variable is a location in a computer that contains data, which
might be changed during the execution of a Ruby script.
There are three aspects to a variable:
- The name of a Ruby variable must start with a
letter (upper or lower case) or underscore and consist entirely of
letters, digits, or underscores.
- There is no limit to the length of a Ruby variable name. However, names
longer than about 12 characters become unwieldy. Here are some
examples of Ruby variable names:
x
x123
customer3
number_of_customers.
Ruby is case sensitive, which means that capitalization matters. The names
number_Of_Customers
and
number_of_customers
represent two different variables.
Ruby uses underscores to separate words in a variable name
An example of a local variable name is number_of_letters.
Although a single underscore is a legal variable name, it is not
recommended.
Names of instance variables are prefixed with an at sign (@):
@count
@person
@grocery_item
For class names, Ruby uses upper camel casing
(first letter uppercase,
each letter of a new word in uppercase, no underscores or embedded spaces).
Examples of variables spelled with upper camel casing:
CustomerAccount, PeopleController.
The address of a variable is the location
in the computer's memory where the data is stored. The Ruby interpreter
keeps a symbol table that maintains the correspondence between the
variable's name and its address, so the programmer need not be
explicity concerned about the address.
The value of a variable is the data currently stored at its address.
Every variable contains the data of an object. Objects can be
from the FixNum, BigNum, String, or Boolean classes. They can also
be from any other class defined by the user or by the Rails software.
A variable can only contain one value at a time.
Variable Declaration
However, the following are not allowed:
s = "328" + 5
Use either of the following instead:
s = "328".to_i + 5
t = "dog" + 5.to_s
The Assignment Operator
- The assignment operator is used to assign a value to a variable.
For example,
x = 5
assigns the value 5 to the variable x.
Question: why is the following not a legal assignment?
5 = x