Stumped by Rspec 2 silly blocks

I know what blocks are and how to utilize the ones that already exist. However, it seems like I do not know how to create them as I’m totally confused by the first problem as well as the instructions and spec errors. I’m hoping that if i get a few pointers on the 1st problem, I can move on through deduction.

The instructions included in the spec file:
# # Topics
# * blocks
# * closures
# * yield
# * loops
This is the most vague set of instructions I have received. i’ve been looking back at the AA section on blocks as well as the codeacademy block section which I’ve already completed but it doesn’t get me any closer to understanding what I’m supposed to do here. There have been no block/proc related exercises except the ones in Rspec. I’m totally floored that the rspec exercises are much more difficult than the regular exercises, I left them till the end because the forking was not working earlier on.

After examining multiple syntax examples of blocks, I wrote a few to see if any would pass the test:

def reverser

result = reverser {|string| string.reverse}

This one does not pass the test despite returning the correct answer on repl and pry. The spec error is:
Failure/Error: expect(result).to eq(“olleh”)

   expected: "olleh"
        got: "hello"

The next one is modeled exactly after the Codeacademy lesson on yielding to blocks:

def reverser(string)
yield string

reverser(“hello”) {|string| string.reverse}

This one also passes on repl and pry, here is the spec error:
Failure/Error: result = reverser do
wrong number of arguments (given 0, expected 1)

Furthermore, both specs includes an essential line that I do not understand at all: some silly block functions reverser reverses the string returned by the default block. What is the default block mentioned in this case? Why do these output correctly on repl and pry but do not pass the test in rspec?

I’m happy for others to chime in here as I by no means close to an expert. I’ll try my best to help though. From your error of wrong number of arguments we can deduce that this is a method that will take no arguments. From the second RSPEC test “reverses each word in the string returned by the default block” we know that each word in the string has to be reversed individually. I believe that the ‘string’ is what is returned from the block. So we get the string by calling the block (yield), you must then reverse each word in said string. I hope this gets you on the right track. I’m not posting the answer incase you don’t want it yet. I think you’ll be able to figure it out, let me know if you need any more help.

It is confusing how blocks are used and tested. It helped me to look at the spec file very carefully and try to replicate how my code is being called exactly. In this case, the spec uses your reverser method like so (the string isn’t passed as an argument). Do your repl tests using that.

reverser do
    "hello dolly"
# => "olleh yllod" 

Your method should look like the one below, except (like Corey said), it should reverse every word in the string separately, not the string as a whole.

def reverser
    yield.reverse  #plus some map/join logic to reverse each word separately

But yea, it isn’t well explained how it works in context / maybe just a confusing topic. Chris Pine’s ruby book has a chapter on blocks and procs (with exercises) that was decent.

Awesome thanks. Your explanation looks super solid. I’ll let you know when I get back to these problems.

EDIT: I managed to solve it by chaining a bunch of splits, joins and reverses to yield. I also tried .map with a small block included. Honestly, I need more practice with yield method chaining specifically so this will be good for me.