Your SlideShare is downloading. ×
0
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Ruby Metaprogramming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ruby Metaprogramming

189

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 …

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
189
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Metaprogramming By: Rajesh Manjunath
  • 2. Agenda - What ? - Concepts of metaprogramming - Concepts that are used in rails(behind the scene) - hook methods
  • 3. What is Metaprogramming ? Code that writes code …..
  • 4. Sharing Behavior 1) Prototypes 2) Inheritance 3) Mixins
  • 5. Prototype Based Programming. - Purer Form of oo programming. - Achieved by using clone method. - Inheriting state and behavior
  • 6. Inheritance - Sharing of methods between classes - Done By Subclassing - Pretty much used in everyday coding.
  • 7. Modules & Mixins - Include (as instances methods) - extend (as class methods)
  • 8. Dynamic Coding - Blocks - define_method - bindings
  • 9. Blocks - {} or do-end - block object can be created (lambda, proc, &param) - all are of same object proc object.
  • 10. Lambda & Proc - Both are proc objects. - params passed to them. - return
  • 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. define_method - Used to create methods on the fly (dynamically). - Dry up code. ex: find_by_*, attr_accessors etc ….
  • 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. Few Other Hook Methods. - class_eval - instance_eval - instance_variable_(get,set)
  • 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. 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. instance_varaible_(get,set) - used to set the instance - used to get the instance methods: instance_varaible_get instance_varaible_set
  • 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.

×