This is the first page of my Learn Ruby in ten pages project.
Keywords
These are the reserved words in Ruby:
| FALSE |
TRUE |
__FILE__ |
__LINE__ |
alias |
and |
BEGIN |
begin |
| break |
case |
class |
def |
defined? |
do |
else |
elsif |
| END |
end |
ensure |
for |
if |
in |
module |
next |
| nil |
not |
or |
redo |
rescue |
retry |
return |
self |
| super |
then |
undef |
unless |
until |
when |
while |
yield |
Identifiers
Variables start with lower-case letter or underscore, constants with upper-case.
Instance variables are prefixed with @
Class variables are prefixed with @@
Global variables are prefixed with $
Class names must be constants (upper case)
Basic data types
Really, there is only one data type in Ruby. Everything is an Object.
But different objects can exhibit different behaviors. Some of the most common and important are implemented as part of the Ruby language.
Numbers
Numbers in Ruby are one of three types:
Fixnum: small integer. The range of integers that can be represented is system dependent. On my Macintosh it's -2 ^ 30 to 2 ^ 30 - 1. x.size returns the number of bytes in the binary representation of x.
Bignum: big integer. The range of Bignum is practically unlimited.
Float: floating point number. System dependent. Uses the underlying architecture's floating point representation.
Expressions involving integers will automatically produce a Fixnum result if Fixnum can represent the value returned; otherwise a Bignum will be returned.
Expressions involving a Float will always return a Float, even if overflow occurs.
Numeric operators are the same for Fixnum and Bignum.
+ - * /
** exponentiation
% modulo
& | bitwise AND and OR
^ bitwise XOR
<< >> bitwise left/right shift
~x one's complement
-x unary minus
x[n] returns the nth bit of x, where x[0] is the LSB
Bignum binary representation is assumed to be bytewise like Fixnum.
x.coerce(y) returns an array containing compatible values of x and y:
if x and y are the same type, they are stored in the array unchanged.
if x and y are different (e.g., Fixnum and Bignum) an array of two Floats is returned.
Comments