Ruby Intro/The Basics Flashcards

1
Q

What is a literal?

A

any notation that lets you represent a fixed value in source code

44 (integer literal)
nil (nil literal)
true (boolean literal)
"hello" (string literal)
[1, 44, 66]  [array literal]
etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a string?

A

a list of characters in a specific sequence

can use single quotes or double quotes
‘hello’
“hello”

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

What is string interpolation?

A

a way to merge ruby code with a string

name = Scott
puts “Hello, #{name}.”

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

Is there a problem with this?

x = 33
puts ‘You have #{x} friends.’

A

Yes.

To use string interpolation, you must use double quotes

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

How do you create a symbol in ruby?

A

Use :

:name
:“hi, everyone”
etc.

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

Why use a symbol?

A

a symbol is used when you want to reference something like a string but don’t ever intend to print it to the screen or change it

sometimes called an “immutable string”

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

What’s an integer?

What’s a float?

A

a whole number without a decimal

a number with a decimal

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

How do you express “nothing”/”completely empty” in ruby?

A

nil

nil is treated as “false” in an if statement

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

Are “false” and “nil” equivalent to each other?

A

no

they are both treated as negative when evaluated in an expression, but:

false == nil
=> false

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

In math operations, what does % do?

A

“modulo operator”

it gives you the remainder of a division

15 % 4
3

10 % 3
1

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

When using the modulo operator, what should you avoid?

A

negative integers

the rules are somewhat complicated, so try to stick with positive integers

if you have to work with negative integers, go look up the rules online

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

Is this OK?

44 == “44”

A

no
you can’t compare different types (integers, strings, etc.)

44 == “44”
=> false

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

What is string concatenation?

A

adding two strings together to make one

“hello” + “hello”
“hellohello”

‘44’ + ‘44’
‘4444’

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

What are these?
What do they do?

.to_i
.to_s
.to_f

A

conversion methods

convert a string/float to an integer
convert a float/integer to a string
convert an integer/string to a float

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

What is an array?

A

a list of data contained in brackets

[true, false, false, true]
[33, 44, 44]
etc.

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

What is this?

[22, 333, 66, 444, 999] [3]

A

an array with an index

[3] references 444 because 444 is located in the 3 index of the array

17
Q

What is a hash?

A

(a.k.a “a dictionary”)

a set of key-value pairs placed inside { }

keys = Jaws, ET
value = 1975, 1982
18
Q

How do you retrieve a value from an array?

A

Use that value’s key (just like using an index with an array)

{:dog => barks, :cat => meows} [:cat]
=> “meows”

19
Q

What is this called?

=>

A

hash rocket

20
Q

What is an expression?

A

anything that can be evaluated

pretty much everything you write in ruby is an expression

expressions in ruby always return a value, even if it’s “nil” or “error”

21
Q

Here is an error message:

SyntaxError: (irb):2: syntax error, etc. etc.

What does the “2” mean?

A

it means your error is on line 2 of your code