Ruby Symbols/Keywords/Data Types Flashcards
alias
Creates an alias for an existing method, operator, or global variable.
Examples:
alias new old
alias :new :old
alias :new, :old
and
Logical operator; same as &&, except “and” has lower precedence.
BEGIN
Code, enclosed in { and }, that will run before the program runs.
begin
Begins a code block or group of statements; closes with “end” (not to be confused with BEGIN and END).
break
Terminates a “while” or “until” loop, or a method inside a block.
case
Compares an expression with one or more matching “when” clauses; closes with end. Can be used with or without a value to match against.
Example: print "Enter a string: " some_string = gets.chomp case when some_string.match(/\d/) puts 'String has numbers' when some_string.match(/[a-zA-Z]/) puts 'String has letters' else puts 'String has no numbers or letters' end
class
Defines a class; closes with end.
def
Defines a method; closes with end.
defined?
Determines if a variable, method, super method, or block exists.
do
Begins a block and executes code in that block; closes with end.
else
Executes if previous conditional from “if”, “elsif”, “unless”, or “when”, is not true.
elsif
Executes if previous conditional from “if” or “elsif” is not true.
END
Code, enclosed in { and }, to run when the program ends.
end
Ends a code block (group of statements) starting with “begin”, “def”, “do”, “if”, etc.
ensure
Always executes at block termination; use after last rescue.
Example:
begin
# something which might raise an exception
rescue SomeExceptionClass => some_variable
# code that deals with some exception
else
# code that runs only if no exception was raised
ensure
# ensure that this code always runs, no matter what
end
false
Logical or Boolean false, instance of FalseClass.
for
Begins a for loop; used with in.
if
Executes code block if true. Closes with end.
module
Defines a module; closes with end.
next
Jumps before a loop’s conditional.
nil
Empty, uninitialized variable, or invalid, but not the same as zero; object of NilClass.
not
Logical operator; same as “!”
or
Logical operator; same as “||”, except “or” has lower precedence.
redo
Jumps after a loop’s conditional.
rescue
Evaluates an expression after an exception is raised; used before ensure.
retry
Repeats a method call outside of rescue; jumps to top of block (begin) if inside rescue.
return
Returns a value from a method or block. May be omitted.
self
Current object (invoked by a method).
super
Calls method of the same name in the superclass. The superclass is the parent of this class.
then
A continuation for if, unless, and when. May be omitted.
true
Logical or Boolean true, instance of TrueClass.
undef
Makes a method in current class undefined.
unless
Executes code block if conditional statement is false.
until
Executes code block while conditional statement is false.
when
Starts a clause (one or more) under case.
while
Executes code while the conditional statement is true.
yield
Executes the block passed to the method.
\a
Bell or alert.
\b
Backspace.
\f
Formfeed.
\n
Newline.
\r
Carriage return.
\t
Tab.
\v
Vertical tab.
\
Backslash
'
Single quote
"
Double quote
: :
Resolution operator. Used to resolve the scope of a constant/static symbol.
[ ]
Array
- *
Exponent
*
Multiplication
/
Division
%
Modulus; returns remainder
+
Addition
-
Subtraction
«
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
> >
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
&
Binary AND
|
Binary OR
>
Greater than
> =
Greater than or equal to
<
Less than
<=
Less than or equal to
Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second.
==
Is equal to
===
Used to test equality within a when clause of a case statement.
!=
Not equal to
=~
Match string to regular expression.
&&
Logical AND
|
Logical OR
. .
Creates a range from start point to end point; inclusive.
. . .
Creates a range from start point to end point; exclusive.