Ruby class override without inheritance -
i've made experiment:
class < hash def foo 'foo' end end class < hash def bar 'bar' end end
so far result expected, second declaration extends first one. surprised of this:
class def call puts foo puts bar end end
the code above works, if declare later. otherwise get:
typeerror: superclass mismatch class
can assume in ruby, safe skipping superclass specification without side effects after making sure "original-first" declaration parsed?
you able declare inheritance on first occurince of class definition, below variants work:
when you've defined same class inheritance:
class < hash end class < hash end
when you've used default inheritance in second case, treated undefined inheritance:
class < hash end class end
when you've used default inheritance in both cases, default inheritance of
object
class:class end class end
and below not:
when you've used default inheritance in first case, , next tried redefine explicitly:
class end class < hash end typeerror: superclass mismatch class
when you've used specified inheritance (in example
string
) in first case, , next tried redefine explicitly (in examplehash
):class < string end class < hash end typeerror: superclass mismatch class
Comments
Post a Comment