RSPEC2 Exercises

I know there are some other similar questions already but they are all from 2017.

Can anyone explain where I should be looking for guidance on how to approach these? I really feel totally lost. It seems that these require a lot of the topics covered in the Procs lesson which was 3 modules back. I have reread the section but I am still very lost. There is a lot of terminology that is undefined or unfamiliar. I did watch the video about Blocks Procs and Lambdas. But I really feel like I need some more guidance on this.

When should something be defined in an argument with an & like &prc vs when should Proc.new be used? Can we see an example of the same thing being done both ways or would that be impractical?

Is &prc always the terminology used? Do people ever put & in front of something other than “prc?” Like, the example for reverser is:
def reverser(&prc)
Why not
def reverser(&str)?
Is that bad formalism?

And are these specs testing for something specific? I can’t even tell what some of them are asking for?

Ex: The “reverser”
Why won’t this work:
def reverser(str)
words = str.split
words.map { |word| word.reverse}.join(" ")
end

What is the spec testing for? This will reverse the string word by word. It works. But it doesn’t pass the spec.

I know this is a long question with a lot of parts, but I really feel like I’ve totally been dropped in completely unfamiliar territory. I can look at the solutions and I have some rudimentary understanding of them. But I feel like I must have really missed something or like this assignment was put in the wrong section of the course or like a lesson was eliminated but the assesement was kept.

Is this supposed to be totally confusing?

Hi Gabe,

Let’s take some of these questions one at a time.

First, a quick review of blocks and procs:

A block is just code. Often, you’ll see it contained in curly braces after a method like array.map: { |num| num * 2 }.
You’ll often also see it in a do/end format:
array.map do |num| num * 2 end

A Proc is an object that stores a block of code. We can think of it as a variable if that helps, but remember that there are some subtle differences.

On to the dreaded &. This symbol in ruby will do two things in the context of blocks and procs: It wraps a block into a proc and vice versa. (unwraps a Proc into a block)

We most often use it in method definitions to store blocks that come in as input as a Proc, like in our reverser method. Our specs are handing us a do/end block that implicitly returns “hello”. We need to et our method know to convert that block into a proc so that we can use it later and we use & to do it.

As far as naming conventions go, we use “prc” as the variable name so that we can easily remember that the variable is a Proc and not a String. Your code will still work if you type it like “&str”, but it will be confusing to read.

So to answer your question about why won’t your code work, it’s because the reverser method is taking a do/end block and not a string as input. You need to update your method to work on the return value of the proc. You can use another keyword to access the return value of the proc, prc.call.

Let us know what other questions you have going forward! I hope this helps.

-Matthew