1 | Someone.xstub do |
Implementation wise, i need a way to find out the methods delared within the proc in order to do further processing. The most straight-forward way is to have a bare minimum object, after instance_eval of the proc, do a diff of the original set of instance methods & the set of methods for this manipulated object. The following illustrates the concept:
1 | (minimalist = Minimalist.new).instance_eval(&block) |
Well, i've in fact considered using BasicObject, but then, it doesn't appear to be as basic as i hope it should it. Thus i decided to handroll my own Minimalist solution:
1 | class Minimalist |
As u can see (in order to prevent any methods clash with user-defined-to-be-stubbed-methods) what i've done is just:
#1. renaming instance_eval & methods to the more cryptic __instance_eval__ & __methods__, and
#2. undefining all methods, except those with prepending & appending __
It works well in ruby-1.8.7, but then, running the above in ruby-1.9.1 displays the following warning:
warning: undefining `object_id' may cause serious problem
With the solution suggested in this discussion, Minimalist is revised to:
1 | class Minimalist |
Now the warning doesn't show. Yet in ruby-1.8.7, i got plenty of eval warnings:
(eval):1: warning: discarding old say
(eval):1: warning: discarding old say
(eval):1: warning: method redefined; discarding old bang
Hmmm ... undaunted, i got this final version of Minimalist:
1 | class Minimalist |
Yet for jruby-1.5.1, it yields:
warning: `instance_eval' should not be aliased
The final final final (i promise) Minimalist becomes:
1 | class Minimalist |
No comments:
Post a Comment