I would use BasicObject#instance_eval
:
Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj’s instance variables. In the version of instance_eval that takes a String, the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors.
ary = [1,1,2,2,3,4,4]
ary.instance_eval do
detect { |elem| count(elem) == 1 } # => 3
end