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
How would you explicitly return a value from a while loop?
nest the while loop within a function
{ |x| asdf }
What do you call this structure?
block
receiver.each {|x| asdf}
What does this return?
receiver
What iterator is used to access the characters in a string?
each_char
What is each_char used for?
To access each character in a string.
What type of block should use do/end?
multi-line blocks
What syntax should be used for MULTI-line blocks: do/end or {…} ?
do/end
What type of block should use {…}
single-line blocks
What syntax should be used for SINGLE-line blocks: do/end or {…} ?
{…}
When passing a block to a method, is the block inside or outside of the method’s calling parenthesis?
outside
When does a method call not require parenthesis?
when it has no arguments
How do you call a method named foo with no arguments?
foo
How do you write a method named foo that accepts a block?
def foo(&prc)
prc.call
end
What does the &prc represent in this code:
def foo(&prc)
prc.call
end
a block to be passed to foo
What’s the difference between a block and a proc?
A block is a term that refers to the code you write, which is wrapped in do/end or {…}.
A Proc is a ruby object that contains the code written in a block.
How can you pass multiple procs to a method?
procs #2+ can be passed as arguments.
What two things does the yield keyword do?
1. When used within a method, it will call Proc.call on a proc passed to a method.