lsegal/yard — Issue #430: define_method calls inside while iterating inside the class
Author: TiagoCardoso1983
Created: 2011-12-21 16:46:17+00:00
State: open

Hi,

I've come across a use case which i hardly find any documentation for. Lets say I want to define functions that basically are written the same way, just certain literals differ. P.ex. this case:

def pigeon
container.pigeon.increment!
end

def lion
container.lion.increment!
end

Now i refactor this to look like this:

%w(pigeon lion).each do |animal|

define_method :"#{animal}" do
container.send("#{animal}").increment!
end

end

Fairly simple. The thing is, i'd like to document these dynamically created functions, and even tough there's an handler for the define_method call, this does not get called when the function is inside the iterator. I couldn't seem to find a good enough workaround other than forget the iterator and define the functions one after the other, copy-pasting til the day i die.

Do you know any workaround, or is this really a missing feature?