Robert said:
Actually, a Ruby method does *always* return an instance (even without
explicit "return"). Conversely there is nothing like a void function /
method - there is always a value returned.
=> nil
This is exactly what happens:
=> 1
IMHO you are worrying too much about this: if a method has a meaningful
return value it should be documented and returned no matter what the
caller does. If he chooses to ignore it that's his choice. My 0.02EUR.
I'm only curious about this due to performance issues not interface
issues. I am sure everybody has seen _Why's Markaby builder. On each
nested element called a fragment object is returned though very rarely
used.
The fragment objects are used for rarely encountered events that
are outside of the standard Markaby pattern and can be used to
rewind the stream and pull out chunks that were already
inserted.
mab = Markaby::Builder.new
mab.html do
div do
div do
p do
"cat" + br + "dog" + br + "cow"
end
end
end
end
generates
<html>
<div>
<div>
<p>
cat <br/> dog <br/> cow
</p>
</div>
</div>
</html>
The html, div, p and br methods all return fragment
objects. However only the fragment objects from the br
calls actually get used. The fragment objects from the
other calls are just thrown away.
In the case of Markaby I suspect there would be some
performance increase ( small maybe ) by changing the
Markaby code to do something different if it knew
beforehand that the fragment object was never required
instead of generating one and giving it straight to
the garbage collector.
Brad