Ruby Metaprogramming

397 views

Published on

This presentation covers about the core concepts of ruby dynamic nature of programming that acts as the foundation for building frameworks, gems etc. This is mainly for people who are working with Ruby on Rails, as this can be used in creating code on the fly and using 'DRY' nature a lot while coding.

Published in: Software, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
397
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
10
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Ruby Metaprogramming

  1. 1. Metaprogramming By: Rajesh Manjunath
  2. 2. Agenda - What ? - Concepts of metaprogramming - Concepts that are used in rails(behind the scene) - hook methods
  3. 3. What is Metaprogramming ? Code that writes code …..
  4. 4. Sharing Behavior 1) Prototypes 2) Inheritance 3) Mixins
  5. 5. Prototype Based Programming. - Purer Form of oo programming. - Achieved by using clone method. - Inheriting state and behavior
  6. 6. Inheritance - Sharing of methods between classes - Done By Subclassing - Pretty much used in everyday coding.
  7. 7. Modules & Mixins - Include (as instances methods) - extend (as class methods)
  8. 8. Dynamic Coding - Blocks - define_method - bindings
  9. 9. Blocks - {} or do-end - block object can be created (lambda, proc, &param) - all are of same object proc object.
  10. 10. Lambda & Proc - Both are proc objects. - params passed to them. - return
  11. 11. Bindings - Captures the current state, and returns as an binding object. - This can be achieved using method “binding”. - Used with “eval” method by passing as an option.
  12. 12. define_method - Used to create methods on the fly (dynamically). - Dry up code. ex: find_by_*, attr_accessors etc ….
  13. 13. code-sample. Usual code: def user; @data[:user]; end def email; @data[:email] end def food; @data[:food]; end Using define_method: %w(user email food).each do |meth| define_method(meth) { @data[meth.to_sym] } end
  14. 14. Few Other Hook Methods. - class_eval - instance_eval - instance_variable_(get,set)
  15. 15. class_eval & instance_eval - These methods are used for defining class and instance methods for a class on the runtime. - class_eval is used for defining instance methods - instance_eval is used for creating the class methods.
  16. 16. Sample Code. class_eval class Person end Person.class_eval do def say_hello "Hello!" end end jimmy = Person.new jimmy.say_hello # "Hello!" instance_eval class Person end Person.instance_eval do def human? true end end Person.human? # true
  17. 17. instance_varaible_(get,set) - used to set the instance - used to get the instance methods: instance_varaible_get instance_varaible_set
  18. 18. Reference. - http://www.jimmycuadra.com/posts/metaprogramming-ruby-class-eval-and-instance-eval - http://ruby-metaprogramming.rubylearning.com/ - http://clayallsopp.com/posts/rubymotion-metaprogramming/ - Dave Thomas Videos.

×