On Mon, Dec 16, 2013 at 9:10 PM, Abinoam Jr. <abinoam / gmail.com> wrote:

> Dear JD JD,
>
> I'm not sure I got what you willing to accomplish.
>
> But... try this.
>
> "hello world".split.map(&:reverse).join(" ")
>
> Step by step would be:
>
> "hello world".split
>  => ["hello", "world"]
>
> "hello world".split.map { |word| word.reverse}
>  => ["olleh", "dlrow"]
>
> "hello world".split.map { |word| word.reverse}.join(" ")
>  => "olleh dlrow"
>
> Is it what you're looking for?
>
> Best regards,
> Abinoam Jr.
>
> On Mon, Dec 16, 2013 at 11:47 PM, JD JD <lists / ruby-forum.com> wrote:
> > Basically, I am working through some puzzles to learn ruby.  I just
> > finished an online class learning this stuff, and want to test my
> > knowledge.
> >
> > Basically, I am working through puzzles based off of "test driven
> > development".
> >
> > I have basically been tested by making methods do things in the past
> > puzzles.
> >
> > However, this current puzzle "puzzles" me in a way.
> >
> > Here is the test:
> >
> >  result = reverse do
> >         "hello world"
> >  end
> >  result.should == "olleh dlrow"
> > end
> >
> > It's basically rspec.  I understand what it is doing on some level.  Its
> > basically taking in a string.  It's breaking down the string with
> > probably a method .each.
> >
> > Most likely its implementing something like this
> >
> > (string).each do |word|
> >     word.reverse
> > end
> >
> > Problem is, I could make the above work if it was asking for me to make
> > a method that takes in an argument.
> >
> > However, it's not asking for it to take in an argument.  So, I am
> > completely lost.
> >
> > Can someone help me make sense of how to make this test pass?  How do I
> > make this work?  I really am not familiar with this topic 100%.  I'm
> > sure I learned about this, but if someone could help me in the right
> > direction I would really appreciate it :).
> >
> > --
> > Posted via http://www.ruby-forum.com/.
>


Given the exercise, it looks at though they want to introduce you to
working blocks. What you typically do with blocks (the do-end or {}
single-line form) is yield them. In this case, take the result of the
yield, then do the word reversal. You might want to check to make sure the
result of the yield is a String, or can be duck typed as one.

I don't want to just give the answer, as what's the fun of that, right? But
the basic structure would look something like this:


def reverse(&block)
  result = yield if block_given?
  # do something with result to get the reversed words
end