Ruby Flashcards

1
Q

How do you create a non-inclusive range from 0 to 4?

A

0…5

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

s = “12345”

s.slice(0…2) = ?

A

“12” (non-inclusive range)

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

How do you interpolate variables in a string?

A

”#{var}asdf”

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

What is this operator called?

”#{thisguy}”

A

string interpolation operator

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

What are the (2,3,4,5,more?) string case manipulation operators?

A

upcase, downcase, capitalize, swapcase

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

What are two ways to capitalize a string?

A

upcase, capitalize

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

How do you lowercase a string?

A

downcase

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

How do you reverse the cases of a string?

A

swapcase

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

s = “asdf”

What’s another way to write s.slice(0..1)?

A

s[0..1]

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

s = “asdf”

What’s another way to write s[0..1] using a method?

A

s.slice(0..1)

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

a = “asdf”

What will a.delete(‘ds’) return?

A

“af”

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

a = “asdf”

Using the delete method on a, how can you return “af” ?

A

a.delete(‘sd’) or a.delete(‘ds’)

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

With two strings, how can you tell which operator will tell you which one comes first alphabetically?

A

str1 < str2

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

What’s this operator called <=> ?

A

The spaceship operator

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

What’s the spaceship operator (and what does it do?)

A

<=>

a <=> b returns:

-1 if a < b
0 if a == b
1 if a > b

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

How do you determine the truthiness of a value?

A

!!value

The first ! returns true/false, the second ! negates this and returns the truthiness.

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

Why would you ever use !!value ?

A

To determine the truthiness of value

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

What does !!nil return?

A

false

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

Two ways to tell if a value is odd:

A

val % 2 == 1

val.odd?

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

Two ways to tell if a value is even:

A

val % 2 == 0

val.even?

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

What’s the opposite of a while loop?

A

An until loop

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

“asdf”.each_char do |ch|
true
end

what does this code return?

A

“asdf”

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

What does an until loop loop until?

A

Until a truthy condition is met.

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

What does a while loop evaluate to?

A

nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How would you explicitly return a value from a while loop?
nest the while loop within a function
26
{ |x| asdf } What do you call this structure?
block
27
receiver.each {|x| asdf} What does this return?
receiver
28
What iterator is used to access the characters in a string?
each_char
29
What is each_char used for?
To access each character in a string.
30
What type of block should use do/end?
multi-line blocks
31
What syntax should be used for MULTI-line blocks: do/end or {...} ?
do/end
32
What type of block should use {...}
single-line blocks
33
What syntax should be used for SINGLE-line blocks: do/end or {...} ?
{...}
34
When passing a block to a method, is the block inside or outside of the method's calling parenthesis?
outside
35
When does a method call not require parenthesis?
when it has no arguments
36
How do you call a method named foo with no arguments?
foo
37
How do you write a method named foo that accepts a block?
def foo(&prc) prc.call end
38
What does the &prc represent in this code: def foo(&prc) prc.call end
a block to be passed to foo
39
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.
40
How can you pass multiple procs to a method?
procs #2+ can be passed as arguments.
41
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. #2. It allows a method to be defined without adding a &proc to its parameters (possibly confusing).
42
["a", "b", "c"].map(&:upcase) In the above code, what does the &: do?
It calls #to_proc on the upcase function, converting it to a Proc
43
If you're calling a single method within a block, what's a syntactic shortcut?
Use "&:" like so: ["a", "b", "c"].map(&:upcase)
44
How do you check to see if a block was passed to a method?
block_given?
45
Where and why would you use "block_given?" ?
block_given? is used within a method that might accept a block. The purpose is to allow a conditional yield, without causing a LocalJumpError like so: ``` def run_block yield end ``` run_block this will cause a LocalJumpError. to avoid this, we rewrite run_block like so: ``` def run_block yield if block_given? end ```
46
How many blocks can you pass to a method?
1
47
What's the syntax for a switch?
``` case value when X puts "a" when Y puts "b" when Z puts "c" else puts "d" end ```
48
Why can you pass an argument to a proc using === ?
For syntactic purposes surrounding a case statement.
49
What are the 2 differences between a proc and a lambda?
#1. A lambda expects to be called with the correct number of arguments. #2. A return statement in a lambda will return as if returning from a method.
50
How would you set up a doubler/tripler closure using a multiple_generator closure?
``` def multiple_generator(m) lambda do |n| n * m end end ``` ``` doubler = multiple_generator(2) tripler = multiple_generator(3) ``` puts doubler[5] puts doubler[10]
51
What are 4 ways of calling a proc named my_proc and passing an argument val?
my_proc.call(val) my_proc.(val) my_proc[val] my_proc === val
52
Why use 'each' instead of 'for' ?
'for' uses a single reference for each element it loops through. if we try to generate references within a for construct, all of those references will be assigned to the final element.
53
(0..10) creates a range of numbers [0,1,2,...,10]. How would you create a range of numbers in reverse order?
Integer#downto i.e. 5.downto(0).to_a will create: [0,1,2,3,4,5]
54
How do you check to see if all elements in an array match a given condition? Example for all_even?
Array#all? arr.all? {|n| n.even? }
55
What does Array#all? do?
Enumerator that ANDs the truthyness of each element of an array.
56
What does Array#non? do?
Enumerator that ANDs the falseness of each element of an array.
57
What does Array#any? do?
Enumerator that ORs the truthyness of each element of an array.
58
How would you check to see if at least one element of an array passes a conditional test?
Array#any?
59
How would you check to see if no elements from an array pass a conditional test?
Array#none?
60
How would you find out how many items in an array match a conditional test?
Pass a block to Array#count
61
What happens when you pass a block to Array#count?
You get back the number of items in the array that, when passed to the block, that have a truthy value
62
Given an array, how would you return a second array containing values that pass a conditional test?
Array#select
63
What does Array#select do?
Returns an array of items that, when passed to select's block, return a truthy value.
64
What's the opposite of Array#select?
Array#reject
65
What's the opposite of Array#reject?
Array#select
66
How would you sort an array by custom criteria?
Array#sort_by
67
What's a synonym for Array#reduce ?
Array#inject
68
What's a synonym for Array#inject ?
Array#reduce
69
What are 3 ways to call Array#reduce to sum an array?
arr. reduce(:+) arr. reduce { |acc, x| acc + x } arr. reduce(0) {|acc, x| acc + x}
70
What method removes trailing whitespace from a string?
String#rstrip
71
How do you declare a hash "hsh" with keys [0,1,2] and values ['zero', 'one', two'] to it?
hsh = {0=>'zero', 1=>'one', 2=>'two'}
72
What is this symbol called, in relation to hashes? =>
hash rocket
73
What does the hash rocket symbol look like?
=>
74
What does Hash#include? test its argument against?
hash.keys
75
What are three synonym methods that check whether their argument is a key in a hash?
has_key? key? include?
76
What method checks to see if a hash has a value?
has_value?
77
Hash.new('blah') What does this do?
It creates a new, empty hash, and sets the default value of new hash elements to 'blah'.
78
How do you set the default value of new hash elements to blah?
Hash.new('blah')
79
What's a good way to set up a counter hash, h ?
h = Hash.new(0)
80
arr.each_with_index {|a, b|} Which variable is the element and which is the index?
a = element b = index
81
How would you open a file and do something to each line?
File.foreach('filename.txt') {|line| ... }
82
How would you slurp an entire file as one long string?
contents = File.read('filename.txt')
83
How do you guarantee that a file with handle f that you've been writing to will be successfully written to disk?
f.close
84
How do you access parameters passed in on the command line? How about the filename?
ARGV (as an array) ARGV[0] is the filename
85
What does ARGV contain?
ARGV contains the parameters passed in on the command line
86
What happens if you pass a block to Array.new?
each element of the array will be assigned the value returned by the block
87
How would you preinitialize each value of an array to some value?
By passing a block to Array.new
88
What's the term to describe multiple assignments done in the same line?
destructuring
89
What property of a symbol prevents it from being changed?
immutability
90
What's the purpose of a symbol
to represent names of things inside the ruby interpreter
91
What class of objects represents things inside the ruby interpreter?
Symbol
92
What's the syntax to define a hash using symbols for this data: "blah" 0 "super" 1
{blah: 0, super: 1}
93
When should a Symbol be used instead of a String?
when representing data that won't be input or output
94
How would you set up a method that took in an options hash and assigned default values to that hash?
``` def meth(options = {}) defaults = {name: 'blah', foo: 'bar'} ``` options = defaults.merge(options) ... end
95
How do you access an array containing the current call stack?
Kernel#caller
96
What's a tight way to refer to a class method my_method in MyClass?
MyClass::my_method
97
What does MyClass::my_method refer to?
a class method
98
How do you delete an item in an array arr at index idx?
arr.delete_at(idx)
99
When reading in a bunch of lines from a text file named 'foo.txt', "\n" shows up at the end of each line. How do you prevent this?
File.readlines("foo.txt" | ).map(&:chomp)
100
What's a faster way to see if a value falls within a range? Why?
Range#cover? Rather than instantiate an object for each value within range, it instantiates only the first and last objects, then uses logic to figure out if a value is included.
101
What does Range#cover? do?
It checks to see if a value falls within a range WITHOUT instantiating more than 2 elements of that range.
102
How do you call a class method from within a class?
Same as outside a class. ClassName.method_name
103
Why are "and" and "or" used for control flow, rather than for logic?
Their precedence is lower than assignment, therefore only the first and/or will be used before the assignment occurs.
104
What's the value of result after this statement? result = true and false and true
true hint: order of operation