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.
What is this?
[22, 333, 66, 444, 999] [3]
an array with an index
[3] references 444 because 444 is located in the 3 index of the array
What is a hash?
(a.k.a “a dictionary”)
a set of key-value pairs placed inside { }
keys = Jaws, ET value = 1975, 1982
How do you retrieve a value from an array?
Use that value’s key (just like using an index with an array)
{:dog => barks, :cat => meows} [:cat]
=> “meows”
What is this called?
=>
hash rocket
What is an expression?
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”
Here is an error message:
SyntaxError: (irb):2: syntax error, etc. etc.
What does the “2” mean?
it means your error is on line 2 of your code