Using Ruby 1.9.2
Problem
Compare the contents, not the results, of the two processes. I understand that the results cannot be tested due to a stopping problem , but this is normal; I still donβt want to test the results.
for example
proc {@x == "x"} == proc {@x == "x"} => false
This returns false because the objects inside the procs do not match.
My clumsy decision
I have a work solution that seems to do what I want, but does not really verify that proc is βequalβ to what I put into it. In my particular case, the format of my procs will always be a logical test for instance variables like this:
{@x == "x" && @y != "y" || @z == String}
I wrote a method that dynamically builds classes and creates instance variables set for given values:
def create_proc_tester(property_value_hash) new_class = Class.new.new new_class.class.class_eval do define_method(:xql?) { |&block| instance_eval &block } end property_value_hash.each do |key, value| new_class.instance_variable_set("@#{key}", value) end new_class end
What can be used something like this:
class Foo attr_accessor :block end foo = Foo.new foo.block = proc {@x == "x" && @y != "y" || @z == String} tester = create_proc_tester(:x => "x", :y => "y", :z => Fixnum) puts "Test #1: #{tester.xql? &foo.block}" tester = create_proc_tester(:x => "x", :y => "x", :z => String) puts "Test #2: #{tester.xql? &foo.block}" > Test #1: false > Test #2: true
.
.
This is all wonderful and wonderful, but I want to know if there is a more efficient, more meta-method that actually checks the contents of proc, and not just the work that solves my specific problem; something that could be used to test any process.
I thought there might be a way to use the Ruby parser to compare something, but I have no idea how to do this. I am studying this now, but thought that I would try to see if anyone here had done this before and knows how to do it. This may be a dead end, although due to the dynamic nature of Ruby, but now that I look now.