F
Fredrik Jagenheim
Hi,
First a little warning, this post is more of a rubberducking session
than a proper question.
I have a rather trivial problem, but since I'm spending 8 hours a day
cursing C++ and far coding in Ruby, I'm afraid I can't think of a
proper Ruby solution, and instead come up with 'this is how it
could've been done in C++'-solutions.
The problem: I have a data set. I'd like to see if this data set
fullfills certain properties, so I did a couple of classes which will
do the matching against these properties:
def MatchA
def MatchA.match?(set)
# Complex calculations on set
true
end
end
def MatchB
def MatchB.match(set)
# More complex calculations on set
true
end
end
Then I'd just do:
puts "It matched A" if MatchA.match?(set)
puts "It matched B" if MatchB.match?(set)
Ok, but now I have 50 of these match classes, so I figured I should
make an array of them:
array_of_match_objects.each{ |match|
puts "It matched #{match.name}" if match.match(set)
}
I would have to change the methods from class to object first, I
guess...
Anyway, then I started to think about using a superclass for these
classes and use Ruby introspection to find all the subclasses and call
match from there. That's when I realized there must be simpler
solution I just can't see...
Perhaps extending the set-class with the methods;
def MatchB
def match_b?
# Complex calculation
end
end
set.extend(MatchB)
puts "Matched B" if set.match_b?
It probably be the best way OO-wise, since it's a property of the Set.
Or am I really wrong here?
//F
First a little warning, this post is more of a rubberducking session
than a proper question.
I have a rather trivial problem, but since I'm spending 8 hours a day
cursing C++ and far coding in Ruby, I'm afraid I can't think of a
proper Ruby solution, and instead come up with 'this is how it
could've been done in C++'-solutions.
The problem: I have a data set. I'd like to see if this data set
fullfills certain properties, so I did a couple of classes which will
do the matching against these properties:
def MatchA
def MatchA.match?(set)
# Complex calculations on set
true
end
end
def MatchB
def MatchB.match(set)
# More complex calculations on set
true
end
end
Then I'd just do:
puts "It matched A" if MatchA.match?(set)
puts "It matched B" if MatchB.match?(set)
Ok, but now I have 50 of these match classes, so I figured I should
make an array of them:
array_of_match_objects.each{ |match|
puts "It matched #{match.name}" if match.match(set)
}
I would have to change the methods from class to object first, I
guess...
Anyway, then I started to think about using a superclass for these
classes and use Ruby introspection to find all the subclasses and call
match from there. That's when I realized there must be simpler
solution I just can't see...
Perhaps extending the set-class with the methods;
def MatchB
def match_b?
# Complex calculation
end
end
set.extend(MatchB)
puts "Matched B" if set.match_b?
It probably be the best way OO-wise, since it's a property of the Set.
Or am I really wrong here?
//F