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:

  1. when you've defined same class inheritance:

    class < hash end  class < hash end 
  2. when you've used default inheritance in second case, treated undefined inheritance:

    class < hash end  class end 
  3. when you've used default inheritance in both cases, default inheritance of object class:

    class end  class end 

and below not:

  1. when you've used default inheritance in first case, , next tried redefine explicitly:

    class end  class < hash end  typeerror: superclass mismatch class 
  2. when you've used specified inheritance (in example string) in first case, , next tried redefine explicitly (in example hash):

    class < string end  class < hash end  typeerror: superclass mismatch class 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -