class Test
def make(name, &bl)
Object.define_method(name) do |*args|
bl
end
end
end
running = true # must be true
kaka = [3,7,12] # must include 12
name = :otto # must be a symbol
# initialize
test = Test.new
# create a test
test.make('check') {|r, k, n|
r && (k.include? 12) && (n.is_a? Symbol)
}
# run the test
p test.check.(running, kaka, name) # true
# another set of data
another_running = true
another_kaka = [12]
another_name = 'erik'
# another test
p test.check.(another_running, another_kaka, another_name) # false
#####################################
# You can also do things like this: #
#####################################
Person = Data.define(:country, :city, :age)
ppl = {
urban: Person.new(:SE, 'uppsala', 34),
emma: Person.new(:SE, 'sjöbo', 16),
linus: Person.new(:UK, 'leeds', 21)
}
peeps = Test.new
peeps.make(:SE) {|age| age > 21 }
peeps.make(:UK) {|age| age > 18 }
# :SE rule is used for Data.country == :SE
# :UK rule is used for Data.country == :UK
ppl.each {|k,v|
p '%s (%s) => %s' % [k, v.age, v.city] if peeps.send(v.country).(v.age)
}