Ruby: Symbol Review Flashcards Preview

Programming > Ruby: Symbol Review > Flashcards

Flashcards in Ruby: Symbol Review Deck (50)
Loading flashcards...
1
Q

\

A

Escape sequence for string: Escapes the “" character.

2
Q

'

A

Escape sequence for single-quoted string. Escape the single quote character.

3
Q

"

A

Escape sequence for double-quoted string. Escape the double quote character.

4
Q

\n

A

Escape sequence for double-quoted string. Starts a new line.

5
Q

\s

A

Escape sequence for double-quoted string. Adds a space.

6
Q

\t

A

Escape sequence for double-quoted string. Adds a tabbed space (~5 spaces)

7
Q

\b

A

Escape sequence for double-quoted string. Deletes a space (backspace function). In IRB, only works with puts.

8
Q

\a

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.

9
Q

\f

A

Escape sequence. Begins a new line at the same point as the previous line ended.

example: puts “Hello\fWorld”

Hello
World
=> nil

10
Q

\v

A

Escape sequence. Begins a new line at the same point as the previous line ended.

example: puts “Hello\fWorld”

Hello
World
=> nil

11
Q

..

A

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]

12
Q

A

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]

13
Q

||

A

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.

14
Q

&&

A

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.

15
Q

not

A

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.

16
Q

!

A

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.

17
Q

or

A

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.

18
Q

and

A

Called Logical AND operator. If both the operands are true then then condition becomes true. Same as ‘&&’ operator.

Example:

(a and b) is true.

19
Q

+

A

Addition - Adds values on either side of the operator.

Example:

a + b will give 30

20
Q

-

A

Subtraction - Subtracts right hand operand from left hand operand.

Example:

a - b will give -10

21
Q

*

A

Multiplication - Multiplies values on either side of the operator.

Example:

a * b will give 200

22
Q

/

A

Division - Divides left hand operand by right hand operand.

Example:

10 / 5 will give 2

23
Q

%

A

Modulus - Divides left hand operand by right hand operand and returns remainder.

Example:

10 % 4 will give 2

24
Q

**

A

Exponent - Performs exponential (power) calculation on operators.

Example:

6**3 will give 6 to the power 3

25
Q

==

A

Checks if the value of two operands are equal or not, if yes then condition becomes true.

Example:

(a == b) is not true.

26
Q

!=

A

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.

27
Q

>

A

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.

28
Q

<

A

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.

29
Q

> =

A

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.

30
Q

<=

A

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.

31
Q
A

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.

32
Q

===

A

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

33
Q

.eql?

A

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.

34
Q

equal?

A

True if the receiver and argument have the same object id.

Example:

a = 2
a.equal?(2)

true

35
Q

=

A

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

36
Q

+=

A

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

37
Q

-=

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

38
Q

*=

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

39
Q

/=

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

40
Q

%=

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.

41
Q

**=

A

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

42
Q

defined?

A

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)

43
Q

? :

A

Ternary Operator; conditional.

If Condition is true ? Then value X : Otherwise value Y

if a == b ? “a”:”b”

44
Q

alias

A

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

45
Q

[ ]

A

Symbol for an array.

46
Q

::

A

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

47
Q

Add-on modifiers (with if statements)

A

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?

48
Q

Toggle-style (adding modifiers after the ‘end’)

A

Example:

if artist == “John Coltrane” artist = “‘Trane”
end unless use_nicknames == “no”

49
Q

Case Expressions

A

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
50
Q

Case Statements Using Then syntax

A
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