R
Robert Dober
Hi list
motivated by a recent discussion on this list and in order to be
functionally compatible with #is_a? for modules I have added
Object#behaves_like?. I did not want to overload Object#is_a? because
traits do not really semantically provide the notion of inheritance.
An important difference to is_a? or Module#=== is that
Object#behaves_like? will always reflect the current state of the
object and the trait in question even if they were
dynamically changed.
E.g.
t = trait {
message :a do :a end
}
o = t.to_object
t.message :b do :b end
o.behaves_like? t # --> false
class << o
def b; 42 end
end
o.behaves_like? t # --> true
You can use Object#behaves_like? with all objects that respond to a
message get_message_names
if it returns an Enumerable yielding strings and symbols to #each, but
of course it is primarily
designed for traits
.
Enjoy
Robert
http://rubyforge.org/frs/?group_id=4642
motivated by a recent discussion on this list and in order to be
functionally compatible with #is_a? for modules I have added
Object#behaves_like?. I did not want to overload Object#is_a? because
traits do not really semantically provide the notion of inheritance.
An important difference to is_a? or Module#=== is that
Object#behaves_like? will always reflect the current state of the
object and the trait in question even if they were
dynamically changed.
E.g.
t = trait {
message :a do :a end
}
o = t.to_object
t.message :b do :b end
o.behaves_like? t # --> false
class << o
def b; 42 end
end
o.behaves_like? t # --> true
You can use Object#behaves_like? with all objects that respond to a
message get_message_names
if it returns an Enumerable yielding strings and symbols to #each, but
of course it is primarily
designed for traits
Enjoy
Robert
http://rubyforge.org/frs/?group_id=4642