Explanation of the metaprogramming in Ruby getting started

Source: Internet
Author: User

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
  1. ClassMetaperson
  2. DefMetaperson. method_missing (methodname, * ARGs) # redefine the ruby built-in method method_missing
  3. Name = methodname. to_s
  4. Begin
  5. # Call the built-in Ruby method class_eval to automatically generate a new method
  6. Class_eval (% Q [
  7. Def# {Name}
  8. Puts '# {name}, # {name}, # {name }...'
  9. End
  10. ])
  11. Rescue
  12. # When an exception occurs, execute the original Ruby method_missing (methodname, * ARGs)
  13. Super(Methodname, * ARGs)
  14. End
  15. End
  16. DefMethod_missing (methodname, * ARGs)
  17. Metaperson. method_missing (methodname, * ARGs) # instance method_missing call class method method_missing
  18. Send (methodname) # Use the ruby built-in send method to call the previously automatically generated Method
  19. End
  20. # The following code is similar ---------
  21. DefMetaperson. modify_method (methodname, methodbody)
  22. Class_eval (% Q [
  23. Def# {Methodname}
  24. # {Methodbody}
  25. End
  26. ])
  27. End
  28. DefModify_method (methodname, methodbody)
  29. Metaperson. modify_method (methodname, methodbody)
  30. End
  31. #---------------------------
  32. End# End metaperson class definition
  33. ClassPerson <metaperson
  34. End
  35. Person1 = person.New
  36. Person2 = person.New
  37. Person1.sleep # => sleep, sleep, sleep...
  38. Person1.running # => running, running, running...
  39. Person1.modify _ method ("Sleep", "puts 'zzz... '") # modify the method sleep
  40. Person1.sleep # => Zzz...
  41. 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
  1. ClassPerson
  2. DefMethod_missing (methodname, * ARGs)
  3. Name = methodname. to_s
  4. OBJ =Self. Inspect
  5. Begin
  6. Self. Instance_eval (% Q [
  7. Def# {Name}
  8. Puts '# {OBJ}, # {name }'
  9. End
  10. ])
  11. Send (methodname)
  12. Rescue
  13. Super(Methodname, * ARGs)
  14. End
  15. End
  16. DefModify_method (methodname, methodbody)
  17. Self. Instance_eval (% Q [
  18. Def# {Methodname}
  19. # {Methodbody}
  20. End
  21. ])
  22. End
  23. End
  24. P1 = person.New
  25. P2 = person.New
  26. P1.sleep #=># <person: 0x296cb14>, sleep
  27. P1.modify _ method (: Sleep, "puts 'this is P1 Zzz ...'")
  28. P1.sleep # => This is P1 Zzz...
  29. P2.sleep #=># <person: 0x296c998>, sleep
  30. P2.modify _ method (: Sleep, "puts 'this is P2 Zzz ...'")
  31. P2.sleep # => This is P2 Zzz...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.