Ruby Flashcards

You may prefer our related Brainscape-certified 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
Q

How would you explicitly return a value from a while loop?

A

nest the while loop within a function

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

{ |x| asdf }

What do you call this structure?

A

block

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

receiver.each {|x| asdf}

What does this return?

A

receiver

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

What iterator is used to access the characters in a string?

A

each_char

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

What is each_char used for?

A

To access each character in a string.

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

What type of block should use do/end?

A

multi-line blocks

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

What syntax should be used for MULTI-line blocks: do/end or {…} ?

A

do/end

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

What type of block should use {…}

A

single-line blocks

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

What syntax should be used for SINGLE-line blocks: do/end or {…} ?

A

{…}

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

When passing a block to a method, is the block inside or outside of the method’s calling parenthesis?

A

outside

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

When does a method call not require parenthesis?

A

when it has no arguments

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

How do you call a method named foo with no arguments?

A

foo

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

How do you write a method named foo that accepts a block?

A

def foo(&prc)
prc.call
end

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

What does the &prc represent in this code:

def foo(&prc)
prc.call
end

A

a block to be passed to foo

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

What’s the difference between a block and a proc?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

How can you pass multiple procs to a method?

A

procs #2+ can be passed as arguments.

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

What two things does the yield keyword do?

A

1. When used within a method, it will call Proc.call on a proc passed to a method.

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

[“a”, “b”, “c”].map(&:upcase)

In the above code, what does the &: do?

A

It calls #to_proc on the upcase function, converting it to a Proc

43
Q

If you’re calling a single method within a block, what’s a syntactic shortcut?

A

Use “&:” like so:

[“a”, “b”, “c”].map(&:upcase)

44
Q

How do you check to see if a block was passed to a method?

A

block_given?

45
Q

Where and why would you use “block_given?” ?

A

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
Q

How many blocks can you pass to a method?

A

1

47
Q

What’s the syntax for a switch?

A
case value
when X
  puts "a"
when Y
  puts "b"
when Z
  puts "c"
else
  puts "d"
end
48
Q

Why can you pass an argument to a proc using === ?

A

For syntactic purposes surrounding a case statement.

49
Q

What are the 2 differences between a proc and a lambda?

A

1. A lambda expects to be called with the correct number of arguments.

50
Q

How would you set up a doubler/tripler closure using a multiple_generator closure?

A
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
Q

What are 4 ways of calling a proc named my_proc and passing an argument val?

A

my_proc.call(val)

my_proc.(val)

my_proc[val]

my_proc === val

52
Q

Why use ‘each’ instead of ‘for’ ?

A

‘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
Q

(0..10) creates a range of numbers [0,1,2,…,10].

How would you create a range of numbers in reverse order?

A

Integer#downto

i.e. 5.downto(0).to_a will create:

[0,1,2,3,4,5]

54
Q

How do you check to see if all elements in an array match a given condition? Example for all_even?

A

Array#all?

arr.all? {|n| n.even? }

55
Q

What does Array#all? do?

A

Enumerator that ANDs the truthyness of each element of an array.

56
Q

What does Array#non? do?

A

Enumerator that ANDs the falseness of each element of an array.

57
Q

What does Array#any? do?

A

Enumerator that ORs the truthyness of each element of an array.

58
Q

How would you check to see if at least one element of an array passes a conditional test?

A

Array#any?

59
Q

How would you check to see if no elements from an array pass a conditional test?

A

Array#none?

60
Q

How would you find out how many items in an array match a conditional test?

A

Pass a block to Array#count

61
Q

What happens when you pass a block to Array#count?

A

You get back the number of items in the array that, when passed to the block, that have a truthy value

62
Q

Given an array, how would you return a second array containing values that pass a conditional test?

A

Array#select

63
Q

What does Array#select do?

A

Returns an array of items that, when passed to select’s block, return a truthy value.

64
Q

What’s the opposite of Array#select?

A

Array#reject

65
Q

What’s the opposite of Array#reject?

A

Array#select

66
Q

How would you sort an array by custom criteria?

A

Array#sort_by

67
Q

What’s a synonym for Array#reduce ?

A

Array#inject

68
Q

What’s a synonym for Array#inject ?

A

Array#reduce

69
Q

What are 3 ways to call Array#reduce to sum an array?

A

arr. reduce(:+)
arr. reduce { |acc, x| acc + x }
arr. reduce(0) {|acc, x| acc + x}

70
Q

What method removes trailing whitespace from a string?

A

String#rstrip

71
Q

How do you declare a hash “hsh” with keys [0,1,2] and values [‘zero’, ‘one’, two’] to it?

A

hsh = {0=>’zero’, 1=>’one’, 2=>’two’}

72
Q

What is this symbol called, in relation to hashes?

=>

A

hash rocket

73
Q

What does the hash rocket symbol look like?

A

=>

74
Q

What does Hash#include? test its argument against?

A

hash.keys

75
Q

What are three synonym methods that check whether their argument is a key in a hash?

A

has_key?

key?

include?

76
Q

What method checks to see if a hash has a value?

A

has_value?

77
Q

Hash.new(‘blah’)

What does this do?

A

It creates a new, empty hash, and sets the default value of new hash elements to ‘blah’.

78
Q

How do you set the default value of new hash elements to blah?

A

Hash.new(‘blah’)

79
Q

What’s a good way to set up a counter hash, h ?

A

h = Hash.new(0)

80
Q

arr.each_with_index {|a, b|}

Which variable is the element and which is the index?

A

a = element

b = index

81
Q

How would you open a file and do something to each line?

A

File.foreach(‘filename.txt’) {|line| … }

82
Q

How would you slurp an entire file as one long string?

A

contents = File.read(‘filename.txt’)

83
Q

How do you guarantee that a file with handle f that you’ve been writing to will be successfully written to disk?

A

f.close

84
Q

How do you access parameters passed in on the command line? How about the filename?

A

ARGV (as an array)

ARGV[0] is the filename

85
Q

What does ARGV contain?

A

ARGV contains the parameters passed in on the command line

86
Q

What happens if you pass a block to Array.new?

A

each element of the array will be assigned the value returned by the block

87
Q

How would you preinitialize each value of an array to some value?

A

By passing a block to Array.new

88
Q

What’s the term to describe multiple assignments done in the same line?

A

destructuring

89
Q

What property of a symbol prevents it from being changed?

A

immutability

90
Q

What’s the purpose of a symbol

A

to represent names of things inside the ruby interpreter

91
Q

What class of objects represents things inside the ruby interpreter?

A

Symbol

92
Q

What’s the syntax to define a hash using symbols for this data:

“blah” 0
“super” 1

A

{blah: 0, super: 1}

93
Q

When should a Symbol be used instead of a String?

A

when representing data that won’t be input or output

94
Q

How would you set up a method that took in an options hash and assigned default values to that hash?

A
def meth(options = {})
  defaults = {name: 'blah', foo: 'bar'}

options = defaults.merge(options)


end

95
Q

How do you access an array containing the current call stack?

A

Kernel#caller

96
Q

What’s a tight way to refer to a class method my_method in MyClass?

A

MyClass::my_method

97
Q

What does MyClass::my_method refer to?

A

a class method

98
Q

How do you delete an item in an array arr at index idx?

A

arr.delete_at(idx)

99
Q

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?

A

File.readlines(“foo.txt”

).map(&:chomp)

100
Q

What’s a faster way to see if a value falls within a range? Why?

A

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
Q

What does Range#cover? do?

A

It checks to see if a value falls within a range WITHOUT instantiating more than 2 elements of that range.

102
Q

How do you call a class method from within a class?

A

Same as outside a class. ClassName.method_name

103
Q

Why are “and” and “or” used for control flow, rather than for logic?

A

Their precedence is lower than assignment, therefore only the first and/or will be used before the assignment occurs.

104
Q

What’s the value of result after this statement?

result = true and false and true

A

true

hint: order of operation