On Sun, 2006-01-08 at 23:47, Steve Litt wrote:
>
> If someone can show me an advantage to adding methods and instance variables
> in real time, and that advantage can't be realized with normal OOP
> techniques, I'll keep an open mind. But unless it offers me a unique benefit
> that I need, I wouldn't do it.
I recently wrote some code where I had to retrieve an attribute from a
REXML attribute list based on its name and namespace. The REXML
attribute list does have a method for retrieving attributes based on
namespace prefix and name, but this is not very useful. A namespace
aware XML processor, such as an XSLT processor, can, without breaking
any rules, change prefixes at will.
I couldn't just subclass the attribute list, because the REXML parser
would not use my subclass when building a node tree from an XML
document. However, I could do this:
module REXML
class Attributes
def get_attribute_ns(namespace, name)
each_attribute() { |attribute|
if name == attribute.name &&
namespace == attribute.namespace()
return attribute
end
}
nil
end
end
end
Which enabled me to write code like this:
actual_attribute =
actual_attributes.get_attribute_ns(expected_namespace, expected_name)
I could have solved the problem with an external helper class instead,
but that would, in my opinion, have been a clumsier solution.
/Henrik
--
http://www.henrikmartensson.org/ - Reflections on software development