Brad Phelan
12/19/2006 3:50:00 PM
Robert Klemme wrote:
> On 19.12.2006 12:21, Brad Phelan wrote:
>> Eric Hodel wrote:
>>> On Dec 19, 2006, at 01:20, Brad Phelan wrote:
>>>
>>>> Is it possible ( and I think not ) that you can discover if any
>>>> return arguments are requested when a method is called.
>>>
>>> Methods only ever return one Object. (Sometimes that Object will be
>>> splatted, giving the illusion of multiple arguments.)
>>
>> True. However that doesn't answer the question as
>> to whether it is possible to discover if that single object is used
>> or not. I suspect Ruby syntax would make this difficult to figure
>> out.
>
> 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.
>
> >> def foo() end
> => nil
> >> foo
> => nil
>
>> If the critical function is called as the last statement in another
>> function or block then the return object can get passed implicitly
>> up the stack until something takes hold of it.
>
> This is exactly what happens:
>
> >> def bar() 1 end
> => nil
> >> def foo() bar() end
> => nil
> >> foo
> => 1
> >> bar
> => 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