Search code examples
rubyproc-object

Ruby - What is this output


I know that this code may be not quite correct:

def print_string(&str)
puts str
end

print_string{"Abder-Rahman"}

But, when I run it, this is what I get:

#<Proc:[email protected]:5>

What is this output?


Solution

  • That's the default string representation of a Proc object. Because "Abder-Rahman" is in braces, Ruby thinks you're defining a block. Did you mean to put str.call inside of your function definition? That should call your block and return the string expression you defined inside it.