I'm working on some homework problems for a ruby course and I've been having some trouble with my answer. Basically I need to build a program that can satisfy these conditions:
describe "reverser" do
it "reverses the string returned by the default block" do
result = reverser do
"hello"
end
result.should == "olleh"
end
it "reverses each word in the string returned by the default block" do
result = reverser do
"hello dolly"
end
result.should == "olleh yllod"
end
end
I puzzled together some code that I feel should satisfy these conditions:
reverser = Proc.new do |string|
words = string.split(/\b/)
answer = ''
i = 0
while i < words.count
answer = answer + words[i].reverse
i += 1
end
answer
end
def reverser
yield
end
Yet when I run the rake, my error tells me I have failed the first condition.
expected: "olleh"
got: "hello"
Is there something I'm missing? Do I just not have a proper understanding of procs?
This question has been asked in some form already by a member named pete and answered quite well by another user named mind.blank. This is the source:
Beginner RSpec: Need help writing Ruby code to pass RSpec tests (Silly Blocks exercise).
mind.blank's code was straightforward and worked properly, but I don't just want to copy it without understanding why mine doesn't work. Thanks in advance for any help.