Search code examples
rubyblock

How to call a string from a block in another file using Ruby


Can't figure out what code to put in "05_silly_blocks" to call the string from the block reverser that is stored in the variable result.

require "05_silly_blocks"

describe "some silly block functions" do    
describe "reverser" do
it "reverses the string returned by the default block" do
result = reverser do
"hello"
end
result.should == "olleh"
end

Also does it make a difference is the block is not stored in a variable like result?


Solution

  • I'm not sure I exactly understand what you are trying to accomplish with the blocks, but if you want to reverse a String in Ruby you can just say "hello".reverse.

    The reverser method would look something like this:

    def reverser
      yield.reverse
    end
    

    This is a dangerous implementation however, as it assumes that whatever is passed to the block will return a String at the very end.