Some people said that the ninth chapter of the metaprogramming routines are not easy to understand. I apologize for the lack of ability to show all aspects of metaprogramming, so I chose to demonstrate the simplest metaprogramming without explaining it.
Below are some comments of the routine, which cannot be understood. For beginners, you know that Ruby is longer than metaprogramming.
Ruby code
- ClassMetaperson
- DefMetaperson. method_missing (methodname, * ARGs) # redefine the ruby built-in method method_missing
- Name = methodname. to_s
- Begin
- # Call the built-in Ruby method class_eval to automatically generate a new method
- Class_eval (% Q [
- Def# {Name}
- Puts '# {name}, # {name}, # {name }...'
- End
- ])
- Rescue
- # When an exception occurs, execute the original Ruby method_missing (methodname, * ARGs)
- Super(Methodname, * ARGs)
- End
- End
- DefMethod_missing (methodname, * ARGs)
- Metaperson. method_missing (methodname, * ARGs) # instance method_missing call class method method_missing
- Send (methodname) # Use the ruby built-in send method to call the previously automatically generated Method
- End
- # The following code is similar ---------
- DefMetaperson. modify_method (methodname, methodbody)
- Class_eval (% Q [
- Def# {Methodname}
- # {Methodbody}
- End
- ])
- End
- DefModify_method (methodname, methodbody)
- Metaperson. modify_method (methodname, methodbody)
- End
- #---------------------------
- End# End metaperson class definition
- ClassPerson <metaperson
- End
- Person1 = person.New
- Person2 = person.New
- Person1.sleep # => sleep, sleep, sleep...
- Person1.running # => running, running, running...
- Person1.modify _ method ("Sleep", "puts 'zzz... '") # modify the method sleep
- Person1.sleep # => Zzz...
- Person2.sleep # => Zzz...
What is the purpose of this routine?
Assume that an nd5 robot in "mechanical Public Enemy" finds that smiling is a good communication behavior during the implementation of "Protection of Human Beings", and thus smile. Although nd5 robots would not be smile before, all nd5 robots could be smile just as soon as an nd5 robot tried smile. Of course, any nd5 robot can evolve smile in the future and share the evolution of smile with other nd5 robots.
Maybe you don't want every nd5 robot to be smile. You like the maverick behavior of an instance object. For example, Sanny and Sanny are really different, strong, and witty, humble (please allow me to use these adjectives on a robot ).
To meet your requirements, try the following program.
Ruby code
- ClassPerson
- DefMethod_missing (methodname, * ARGs)
- Name = methodname. to_s
- OBJ =Self. Inspect
- Begin
- Self. Instance_eval (% Q [
- Def# {Name}
- Puts '# {OBJ}, # {name }'
- End
- ])
- Send (methodname)
- Rescue
- Super(Methodname, * ARGs)
- End
- End
- DefModify_method (methodname, methodbody)
- Self. Instance_eval (% Q [
- Def# {Methodname}
- # {Methodbody}
- End
- ])
- End
- End
- P1 = person.New
- P2 = person.New
- P1.sleep #=># <person: 0x296cb14>, sleep
- P1.modify _ method (: Sleep, "puts 'this is P1 Zzz ...'")
- P1.sleep # => This is P1 Zzz...
- P2.sleep #=># <person: 0x296c998>, sleep
- P2.modify _ method (: Sleep, "puts 'this is P2 Zzz ...'")
- P2.sleep # => This is P2 Zzz...