Ruby Intro/The Basics Flashcards
What is a literal?
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.
What is a string?
a list of characters in a specific sequence
can use single quotes or double quotes
‘hello’
“hello”
What is string interpolation?
a way to merge ruby code with a string
name = Scott
puts “Hello, #{name}.”
Is there a problem with this?
x = 33
puts ‘You have #{x} friends.’
Yes.
To use string interpolation, you must use double quotes
How do you create a symbol in ruby?
Use :
:name
:“hi, everyone”
etc.
Why use a symbol?
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”
What’s an integer?
What’s a float?
a whole number without a decimal
a number with a decimal
How do you express “nothing”/”completely empty” in ruby?
nil
nil is treated as “false” in an if statement
Are “false” and “nil” equivalent to each other?
no
they are both treated as negative when evaluated in an expression, but:
false == nil
=> false
In math operations, what does % do?
“modulo operator”
it gives you the remainder of a division
15 % 4
3
10 % 3
1
When using the modulo operator, what should you avoid?
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
Is this OK?
44 == “44”
no
you can’t compare different types (integers, strings, etc.)
44 == “44”
=> false
What is string concatenation?
adding two strings together to make one
“hello” + “hello”
“hellohello”
‘44’ + ‘44’
‘4444’
What are these?
What do they do?
.to_i
.to_s
.to_f
conversion methods
convert a string/float to an integer
convert a float/integer to a string
convert an integer/string to a float
What is an array?
a list of data contained in brackets
[true, false, false, true]
[33, 44, 44]
etc.