Wow.  I liked that idea so much that I whipped together a script to
prove to myself it works.  Damn do I love Ruby...  This is of course
with just a single class, but it is easy to see the power in something
along these lines, being able to have a mixin module or parent class
which defines an interface, and children that implement different
methods for different rules.


class Test
    attr_accessor :properties

    def initialize default_properties=Hash.new
        @properties = default_properties
    end

    def has_property? key
        @properties.has_key? key
    end

    def property_is? key, value
        has_property?(key) && @properties[key] == value
    end

    def evaluate_condition(closure = nil, &block)
        return instance_eval(&closure) if closure.kind_of? Proc
        return instance_eval(&block) if block_given?
        nil
    end
end

test1 = Test.new({'color' => 'red', 'shape' => 'apple', 'taste' =>
'yum', 'fans' => 'none'})
test2 = Test.new({'color' => 'black', 'shape' => 'car', 'engine' =>
'ass kickin', 'fans' => 'several'})
test3 = Test.new({'color' => 'white', 'shape' => 'ibook', 'engine' =>
'g3', 'fans' => 'none'})
test_objects = [test1, test2, test3]

car_test = proc {has_property?('engine') && property_is?('shape', 'car')}
apple_test = proc {property_is?('shape', 'apple')}
corporeal_test = proc {has_property?('shape')}

puts 'testing for a car...'
test_objects.each do |object| puts
object.evaluate_condition(car_test).inspect end

puts 'testing for an apple...'
test_objects.each do |object| puts
object.evaluate_condition(apple_test).inspect end

puts 'testing for corporeal objects...'
test_objects.each do |object| puts
object.evaluate_condition(corporeal_test).inspect end

puts 'block test...'
test_objects.each do |object| puts (object.evaluate_condition
{has_property?('color') && property_is?('fans', 'none')}).inspect end


On Sun, 30 May 2004 02:08:38 +0900, Bill Atkins <dejaspam / batkins.com> wrote:
> 
> How about just a simple proc object?
> 
> my_set.rules = proc { !name("orange") && hasProperty("color") &&
>  (hasProperty("size") || hasProperty("weight")) }
> 
> Then just do something like obj.instance_eval rules for whatever
> object has the methods in your example.
> 
> Bill
> 
> Florian Weber <csshsh / structbench.com> wrote in message news:<D83A07D2-AE5C-11D8-A68B-000A95BD142E / structbench.com>...
> 
> 
> > hi!
> >
> > im trying to define a set of rules with ruby, however i cant find a more
> > ruby-like way to do so..
> >
> > instead of doing something like
> >
> > my_set.add(NotNamedRule("orange").new)
> > my_set.add(HasPropertyRule("color").new)
> > my_set.add(OrRule(HasPropertyRule("size").new,
> > HasPropertyRule("weight").new))
> >
> > (i know this is a horrible example. excuse the awful 'design'. its
> > justs to illustrate
> > what i not wanna have ; )
> >
> > i wanna do something like
> >
> > my_set.rules = !name("orange") && hasProperty("color") &&
> > (hasProperty("size") || hasProperty("weight"))
> >
> > can anybody think of a nice way to do this?
> >
> > thanks a lot for any feedback!
> >
> > ciao!
> > florian
> 
>