I tried this code:
def m
return yield if block_given?
"no block"
end
puts m do
x = 2
y = 3
x*y
end
How come the output is "no block"? What's wrong with my way of constructing the block to m? {"testing"} will work.
I tried this code:
def m
return yield if block_given?
"no block"
end
puts m do
x = 2
y = 3
x*y
end
How come the output is "no block"? What's wrong with my way of constructing the block to m? {"testing"} will work.
Add parentheses to puts
puts(m do
x = 2
y = 3
x * y
end)
The output is 6.
Your code is equivalent to
puts(m) do
x = 2
y = 3
x * y
end
Remove the puts
:
def m
return yield if block_given?
"no block"
end
m do
x = 2
y = 3
x*y
end
It will always return the last statement in the m
block.