Ruby: Symbol Review Flashcards
\
Escape sequence for string: Escapes the “" character.
'
Escape sequence for single-quoted string. Escape the single quote character.
"
Escape sequence for double-quoted string. Escape the double quote character.
\n
Escape sequence for double-quoted string. Starts a new line.
\s
Escape sequence for double-quoted string. Adds a space.
\t
Escape sequence for double-quoted string. Adds a tabbed space (~5 spaces)
\b
Escape sequence for double-quoted string. Deletes a space (backspace function). In IRB, only works with puts.
\a
Escape sequence. Sends an alert to the user. The alert may be dependent on the user’s computer - it may flash, be an audio alert, etc.
\f
Escape sequence. Begins a new line at the same point as the previous line ended.
example: puts “Hello\fWorld”
Hello
World
=> nil
\v
Escape sequence. Begins a new line at the same point as the previous line ended.
example: puts “Hello\fWorld”
Hello
World
=> nil
..
The two dot construction for a Ruby Range means INCLUSIVE of the final character.
Example:
(1..10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
…
The three dot construction for a Ruby Range means EXCLUSIVE of the final character.
Example:
(1…10)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
||
Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Same as ‘or’ operator.
Example:
(a || b) is true.
&&
Called Logical AND operator. If both the operands are non zero then then condition becomes true. Same as ‘and’ operator.
Example:
(a && b) is true.
not
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
Example:
not(a && b) is false.
!
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
Example:
!(a && b) is false.
or
Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Same as ‘||’ operator.
Example:
(a or b) is true.
and
Called Logical AND operator. If both the operands are true then then condition becomes true. Same as ‘&&’ operator.
Example:
(a and b) is true.
+
Addition - Adds values on either side of the operator.
Example:
a + b will give 30
-
Subtraction - Subtracts right hand operand from left hand operand.
Example:
a - b will give -10
*
Multiplication - Multiplies values on either side of the operator.
Example:
a * b will give 200
/
Division - Divides left hand operand by right hand operand.
Example:
10 / 5 will give 2
%
Modulus - Divides left hand operand by right hand operand and returns remainder.
Example:
10 % 4 will give 2
**
Exponent - Performs exponential (power) calculation on operators.
Example:
6**3 will give 6 to the power 3
==
Checks if the value of two operands are equal or not, if yes then condition becomes true.
Example:
(a == b) is not true.
!=
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.
Example:
(a != b) is true.
>
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
Example:
(8 > 10) is not true.
<
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
Example:
(8 < 10) is true.
> =
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
Example:
(8 >= 10) is not true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
Example:
(8 <= 10) is true.
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.
The spaceship operator is used to make comparisons. The results are given relative to zero.
Asking, which side do I belong to? LEFT OR RIGHT?
Example:
1 2 will give -1
Because 1 is LESS than 2, it is -1.
2 1 will give 1
Because 2 is GREATER than 1, it is 1.
===
Used to test equality within a when clause of a case statement.
Example:
(1…10) === 5 returns true.
[Does any number in the range == 5?] true
.eql?
True if the receiver and argument have both the same type and equal values.
Example:
1 == 1.0 returns true, but 1.eql?(1.0) is false.
equal?
True if the receiver and argument have the same object id.
Example:
a = 2
a.equal?(2)
true
=
Simple assignment operator, Assigns values from right side operands to left side operand.
Example:
c = a + b will assign value of a + b into c
+=
Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand.
Example:
c += a is equivalent to c = c + a
-=
Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand.
Example:
c -= a is equivalent to c = c - a
*=
Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand.
Example:
c *= a is equivalent to c = c * a
/=
Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand.
Example:
c /= a is equivalent to c = c / a
%=
Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand.
Example:
c %= a is equivalent to c = c % a
Further Explanation:
a = 10
a % 4
=> 2
if you all the variable “a” again, it will be still be 10, because it wasn’t assigned.
However, if you use:
a %= 4
=> 2
Then a has been set to 2.
**=
Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand.
Example:
c **= a is equivalent to c = c ** a
defined?
defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined. It returns a description string of the expression, or nil if the expression isn’t defined.
Example:
foo = 42
defined? foo # => “local-variable”
defined? $_ # => “global-variable”
defined? bar # => nil (undefined)
defined? method_call # True if a method is defined
defined? puts # => “method”
defined? puts(bar) # => nil (bar is not defined here)
defined? unpack # => nil (not defined here)
? :
Ternary Operator; conditional.
If Condition is true ? Then value X : Otherwise value Y
if a == b ? “a”:”b”
alias
Alias renames and duplicates (gives an alias to), but not replaces, a method name.
Example:
class User
def full_name
puts “Johnnie Walker”
end
alias name full_name
end
User.new.name #=>Johnnie Walker
[ ]
Symbol for an array.
::
The :: symbol is used to access variables that belong to a module.
For example:
module ExampleModule tangerine = "orange-like fruit"
then on another sheet we require that.
require ‘.examplemodule’
ExampleModule::tangerine
^ to call variable
Add-on modifiers (with if statements)
Ruby Statement modifiers let you tack conditional statements onto the end of a normal statement:
Examples:
mon, day, year = $1, $2, $3 if date =~ /(\d\d)-(\d\d)-(\d\d)/
puts “a = #{a}” if $DEBUG
print total unless total.zero?
Toggle-style (adding modifiers after the ‘end’)
Example:
if artist == “John Coltrane” artist = “‘Trane”
end unless use_nicknames == “no”
Case Expressions
2 types of case: the first, similar to a series of if statements, the second, you specify a target at the top of the case:
Example 1:
case when song.name == "Misty" puts "Not again!" when song.duration > 120 puts "Too long!" when Time.now.hour > 21 puts "It's too late" else song.play end
Example 2:
case command when "debug" dump_debug_info dump_symbols when /p\s+(\w+)/ dump_variable($1) when "quit", "exit" exit else print "Illegal command: #{command}" end
Case Statements Using Then syntax
kind = case year when 1850..1889 then "Blues" when 1890..1909 then "Ragtime" when 1910..1929 then "New Orleans Jazz" when 1930..1939 then "Swing" when 1940..1950 then "Bebop" else "Jazz" end