Ruby Flashcards
How do you create a non-inclusive range from 0 to 4?
0…5
s = “12345”
s.slice(0…2) = ?
“12” (non-inclusive range)
How do you interpolate variables in a string?
”#{var}asdf”
What is this operator called?
”#{thisguy}”
string interpolation operator
What are the (2,3,4,5,more?) string case manipulation operators?
upcase, downcase, capitalize, swapcase
What are two ways to capitalize a string?
upcase, capitalize
How do you lowercase a string?
downcase
How do you reverse the cases of a string?
swapcase
s = “asdf”
What’s another way to write s.slice(0..1)?
s[0..1]
s = “asdf”
What’s another way to write s[0..1] using a method?
s.slice(0..1)
a = “asdf”
What will a.delete(‘ds’) return?
“af”
a = “asdf”
Using the delete method on a, how can you return “af” ?
a.delete(‘sd’) or a.delete(‘ds’)
With two strings, how can you tell which operator will tell you which one comes first alphabetically?
str1 < str2
What’s this operator called <=> ?
The spaceship operator
What’s the spaceship operator (and what does it do?)
<=>
a <=> b returns:
-1 if a < b
0 if a == b
1 if a > b
How do you determine the truthiness of a value?
!!value
The first ! returns true/false, the second ! negates this and returns the truthiness.
Why would you ever use !!value ?
To determine the truthiness of value
What does !!nil return?
false
Two ways to tell if a value is odd:
val % 2 == 1
val.odd?
Two ways to tell if a value is even:
val % 2 == 0
val.even?
What’s the opposite of a while loop?
An until loop
“asdf”.each_char do |ch|
true
end
what does this code return?
“asdf”
What does an until loop loop until?
Until a truthy condition is met.
What does a while loop evaluate to?
nil