w1d4 Flashcards

1
Q

What are blocks useful for?

A

To allow the user to customize what a method will do.

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

T/F: Use single-line {braces} for one-line blocks, and do-end for multi-line blocks.

A

T

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

T/F: When blocks are passed to a method, they are given in the list of arguments.

A

F; They are given OUTSIDE the argument:

  1. times { puts “Hey, friend!” } # don’t need parens when no args
  2. times() { puts “Hey, friend!” } # if parens used block is given outside parens
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the following code:

def maybe(flag, &prc)
prc.call if flag
end

A

Declares a method, ‘maybe’, which takes a ‘flag’ argument as well as a block argument.
The ‘&prc’ is the special syntax for setting up a method to take a block. ‘prc.call’ runs the code inside the block.

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

What is the difference between a block and a proc?

A

A block is the Ruby code you write; it is not a real Ruby object.

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

How can you pass multiple procs as arguments?

A

Only add the ampersand to the last proc:

def chain_blocks(start_val, proc1, proc2, &proc3)

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

What happens when you call a proc with arguments?

A

Any arguments are passed into a |piped| variable(s) in the block, if they were declared. Any excess arguments are ignored.

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

What is the advantage of ‘yield’ ?

A

It allows us to pass a block to a method without needing to declare it:

def maybe(flag)
  yield if flag
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the two methods to check if a block was given as an argument?

A

prc.nil? - used if a block has explicity been declared as a parameter

block_given? - used if a block has not been explicitly declared.

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

T/F: You should explicitly return from within a block.

A

F; Do not do this; the last value is implicitly returned from the block. Return will exit from the whole function, not just the block where it is called.

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

If we want to pass a named block to a function, what syntax should we use?

A

Add an ampersand to the block name:

1, 2, 3].map(&add_one) # => [2, 3, 4]

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

What does the following shorthand stand for:

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

A

[“a”, “b”, “c”].map { |s| s.upcase }

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

How can we convert a named method to a proc?

A

proc_name = :method_name.to_proc

proc_name.call(param)

[“Hello”, “Goodbye”].map(&:proc_name) # => [[“H”, “o”], [“G”, “e”]]

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

What are the three uses of ‘&’

A

Converts blocks to procs
Converts method names (passed as symbols) to procs
Converts procs to blocks

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

How can we override the ‘method_missing(method_name)’ method, and what does this accomplish?

A

It allows us to redefine what a method does when it receives a method which is not defined. By default, it sends an error. This, in essence, can allow us to define an infinite number of methods, if we change the return value of method_missing.

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

What is a ‘dynamic finder’?

A

Rather than create a method for every single possible way to search (which is almost infinite), Rails overrides the #method_missing method, and for find_by_* methods, it then parses the method name and figures out how it should perform the search.

17
Q

Explain the following code:

class User
  def method_missing(method_name, *args)
    method_name = method_name.to_s
    if method_name.start_with?("find_by_")
      # attributes_string is, e.g., "first_name_and_last_name"
      attributes_string = method_name[("find_by_".length)..-1]
      # attribute_names is, e.g., ["first_name", "last_name"]
      attribute_names = attributes_string.split("_and_")
  unless attribute_names.length == args.length
    raise "unexpected # of arguments"
  end

  search_conditions = {}
  attribute_names.each_index do |i|
    search_conditions[attribute_names[i]] = args[i]
  end

  # Imagine search takes a hash of search conditions and finds
  # objects with the given properties.
  self.search(search_conditions)
else
  # complain about the missing method
  super
end   end end
A

A dynamic finder in Rails; used for methods that contain ‘find_by’ as well as ‘and’. This allows you to use just about any search method you can define to return a logical result (by searching for what is in the method you define by its title)

18
Q

What is a base case?

A

The smallest (most trivial and direct) subproblem in a recursive method.

19
Q

What is the key to use inductive reasoning for recursion?

A

The key to applying inductive reasoning to solve problems is to (a) identify how you can grow a smaller solution into a bigger solution, and (b) identify base cases which will be the “foundation” of your tower of recursion.

20
Q

How should you decide when to use recursion?

A

Decide if it would be better to solve it iteratively or recursively in your own mind.

21
Q

What is a stack frame, and what happens when you have too many?

A

Each function call in a recursive method adds a stack frame, and when there is not enough memory to hold any more, your program crashes due to stack overflow.

22
Q

What are four strategies for programming recursively?

A

Map out a recursive decomposition (sketch it out)

Identify the base case(s)

Think one level up from the base case (will the call one level up from the base case return the right thing?)

Ensure that your return values from any case (base case or otherwise) are always of the same type (everything is returning from the same method)