Ruby Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

literal

A

Any notation that lets you represent a fixed value

‘Hello, world!’ // string literal
375 // integer literal
3.141528 // float literal
true // boolean literal
{ ‘a’ => 1, ‘b’ => 2 } // hash literal
[ 1, 2, 3 ] // array literal
:sym // symbol literal
nil // nil literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

string

A

list of characters in a specific sequence
written with either single or double quotes
if using single quotes, then single quotes within the text are indicated with escaping: ‘The man said, 'Hi there !' ‘
double quotes allow string interpolation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

:symbol

A

indicated with leading “:”
Used to reference something like a string, but don’t intend to print to screen or change.
Referred to as immutable, although that’s not technically correct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

numbers

A

Integer (whole numbers)

Float (with decimals)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

nil

A

indicates “nothing”

can check for nil with .nil?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

modulo, remainder, divmod

A

Return either modulus or an array containing the quotient and modulus
num.divmod(numeric) → [quotient, modulus]
num % divisor → remainder
num.remainder(numeric) → remainder

a	b	a % b (modulo)	a.remainder(b)	a.divmod(b)
17	5	2	2	[3, 2]
17	-5	-3	2	[-4, -3]
-17	5	3	-2	[-4, 3]
-17	-5	-2	-2	[3, -2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

modulo, remainder, divmod

A

Return either modulus or an array containing the quotient and modulus
num.divmod(numeric) → [quotient, modulus]
num % divisor → remainder
num.remainder(numeric) → remainder

a b a % b (modulo) a.remainder(b) a.divmod(b)
17 5 2 2 [3, 2]
17 -5 -3 2 [-4, -3]
-17 5 3 -2 [-4, 3]
-17 -5 -2 -2 [3, -2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

string.to_i

A

converts string to integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

expressions and return

A

an expression always returns a value in Ruby, even if that value is nil or an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Variable Scope

A

Defined by where the variable is initialized or created. In Ruby, variable scope is defined by a method definition or by a block.
Inner scope can access variables initialized in an outer scope, but not vice versa.

When is a {} or do end piece of code not create a new scope?
When that code is part of the Ruby language, rather than a method.
Example: for I in arr {something} is not changing scope.
5.times { |x| something } is a method (times), and thus changes scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Variable Types

A
Constants     All letters capitalized.  Can be changed, but Ruby will warn at run time.  Scope is global
Global          $var.  Are available throughout app, and override all scope boundaries.  Not typically used in Ruby. 
Class            @@var.  Available in a class and each instances. Not created within the class instance, but in the class.
Instance        @var.  Available in current instance of parent class.  Some can cross scope boundaries.
Local             Most common variable, lower case with _ separating words, not starting with symbol.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

method variables

A

Variables do not enter a method unless passed in, and do not leave unless returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

p
print
puts

A

print sends output with no additional spaces or line returns
puts sends output with a line return at the end
p sends output in the form the programmer wants to see it, e.g. with brackets, quotes etc., to help with validating that the output is in the form expected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

call stack

A

call stack is the point in time: the methods, blocks, procs, and lambdas currently running
limited to 10,000 stack entries, will return “SystemStackError” if exceeded.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Conditionals keywords and operators

A

If, else, elsif, end, and unless keywords

, <=, >=, ==, !=, &&, ||

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

conditional syntax

A
if conditional
  action
elsif conditional
  action
else conditional
  action
end

if conditional then action end

action if conditional

16
Q

conditional operators and different data types

A

<=, , >= are not able to compare different types, e.g. ‘4’ and 4
== and != can compare strings and numbers

17
Q

comparison operator order of precedence

A
  1. <=, , >=
  2. ==, !=
  3. &&
  4. ||
18
Q

Ternary operator

A

conditional ? action if true : action if false

19
Q

case statement syntax

A
case 
when 
  action
when 
  action
else
  action
end
case
when condition
  action
when condition
  action
else
  action
end
20
Q

loop method

A

Simplest loop
loop do
action
end

-or-

loop { action }

21
Q

While loops

A

while condition
action
end

NOT a method, so no separate scope within the loop

22
Q

Until Loop

A

Until condition
action
end

NOT a method, so no new scope created within the loop.

23
Q

do / while loops

A
loop do
  action
  if condition
    break
  end
end

-eg-

loop do
  puts "Do you want to do that again?"
  answer = gets.chomp
  if answer != 'Y'
    break
  end
end
24
Q

for loops

A

No new scope when running

for x in y do
action
end

-e.g.-

x = [1, 2, 3, 4, 5]

for i in x.reverse do
puts i
end

puts “Done!”

25
Q

.each method

A

iterates over each element in a

26
Q

=~

A

Pattern matching or regex operator

use: “basketball” +~ /b/ will return 0 because the first b is in the 0 position