Reconstruction of meta programming

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Reconstruction (two) meta programming


This is the most awesome thing I have learned in the three days to play Ruby, unable to find the corresponding word used to describe the beauty of rhythm, hope. The operation itself when the code is run, this point is like in artificial intelligence, at run time, continuous learning, the robots of the future can not? This can be done the language is not much, Ruby a, Python a. Only the characteristics on Python, like a little bit neither fish nor fowl, the characteristic of Python is simple. Discharge is a kind of interesting things on the Ruby, we can continue to refactor the code, we simplify the design.


Yuan Biancheng metaprogramming

Meta programming is to write code to code
Some reference books is derived from the "Ruby programming", the advantages and disadvantages of this is not discussed, the advantage is that you can simplify the existing code duplication, disadvantage is the dynamic may need to consume CPU.
I think I just learned dynamic method among them, just because some things may not be easy to understand, or do not want to use technology. People have faith, we for technology and so. It is to play the new technology of the time, although there are many chapters need to understand, but let's tidy up these ideas.

I don't like Java because too many repetitive code, and the need to compile, of course I feel or scripting language + editor is more suitable for me. (reprinted retained Phodal's Blog Phodal's zenthink)


Meta programming trip


Repeat repeat

For all my code, I really have a headache grade, simplify the part model
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. def get_weekday_counts
  2. week_results=@week_results
  3. result=Array.new()
  4. i=0
  5. weekday_counts=0
  6. while i5
  7. weekend_counts=weekend_counts+1
  8. end
  9. i=i+1
  10. end
  11. return weekend_counts
  12. end

A little put fine timber to petty use feeling, but used to study or very good examples, and then step by step, in fact, the original code can be directly used to return two value to solve, this is another advantage of Ruby.
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. def self.define_component(name)
  2. define_method(name){
  3. i=0
  4. day=DayCount.new(0,0)
  5. while i<@week_results.size
  6. @result=@result.push(@week_results[i][5])
  7. sum=@week_map[@week_results[i][5]]
  8. if sum>5
  9. day.weekend=day.weekend+1
  10. elsif sum<=5
  11. day.weekday=day.weekday+1
  12. end
  13. i=i+1
  14. end
  15. day.send"#{name}"
  16. }
  17. end
  18. self.define_component:weekday
  19. self.define_component:weekend


And we in the last article on conditional statements made a simple reconstruction, here not much said. The original code like even more good, the problem is if we were extended to him, if it is not so simple, just only for weekdays and weekends, it is not so important.


Meta programming steps



The expansion of existing code

[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. def get_week_counts
  2. week_results=@week_results
  3. result=Array.new()
  4. i=0
  5. weekday_counts=0
  6. while i5
  7. weekend_counts=weekend_counts+1
  8. end
  9. i=i+1
  10. end
  11. return weekend_counts
  12. end


So we find the repeated part of this.

Dynamic generation method

[ruby] view plaincopyprint?To see the code in CODEA piece of code to me


  1. def self.define_component(name)
  2. define_method(name){
  3. i=0
  4. day=DayCount.new(0,0)
  5. while i<@week_results.size
  6. @result=@result.push(@week_results[i][5])
  7. sum=@week_map[@week_results[i][5]]
  8. if sum>5
  9. day.weekend=day.weekend+1
  10. elsif sum<=5
  11. day.weekday=day.weekday+1
  12. end
  13. i=i+1
  14. end
  15. day.send"#{name}"
  16. }
  17. end
  18. self.define_component:weekday
  19. self.define_component:weekend

Dynamic generation after the code shown above.

Whiteboard and summary

Use the whiteboard and dynamic proxy is a good method, the problem is those other languages whether you understand or not, so here is the dynamic method, as to whether or not to use the dynamic distribution will differ from man to man.
Ruby programming is very interesting, isn't it?
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Julia at January 10, 2014 - 12:31 AM