Top Banner
Beauty of Metaprogramming in Ruby S. M. Ashif Manjur @Nascenia
25

Beauty of metaprogramming

Dec 06, 2014

Download

Documents

Ashif Manjur

Basic introduction to ruby metaprogramming. Big thanks to Dave Thomas for his tutorials on metaprogramming
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Beauty of metaprogramming

Beauty of Metaprogrammingin Ruby

S. M. Ashif Manjur@Nascenia

Page 2: Beauty of metaprogramming

Topics I will touch…

•What is Metaprogramming?• Ruby Object Model• Sharing functionalities• instance_eval and class_eval•Magic of method_missing

Page 3: Beauty of metaprogramming

WHAT IS METAPROGRAMMING?

“Writing code that writes code”

Page 4: Beauty of metaprogramming

METHOD LOOKUP CHAIN

Page 5: Beauty of metaprogramming

CLASSES

• Classes are always open• Class definitions are live• Classes are objects• Every method call has a receiver• Anonymous or ghost classes

Page 6: Beauty of metaprogramming

OPEN CLASSES

CONSOLE OUTPUT:  $$$ CAR

Page 7: Beauty of metaprogramming

AS UNCLE BEN SAYS…

“with great power comes great responsibility”

Page 8: Beauty of metaprogramming

MONKEY PATCHING

CONSOLE OUTPUT:  $$$ CAR

Page 9: Beauty of metaprogramming

DEFINITIONS ARE LIVE

Page 10: Beauty of metaprogramming

CLASSES ARE OBJECTS

Page 11: Beauty of metaprogramming

SELF AS A RECEIVER

Page 12: Beauty of metaprogramming

SINGLETON METHODS

Page 13: Beauty of metaprogramming

GHOST CLASSES

Page 14: Beauty of metaprogramming

CLASS METHODS ARE SAME

Page 15: Beauty of metaprogramming

SHARING FUNCTIONALITY

• Prototype• Inheritance•Mixin

Page 16: Beauty of metaprogramming

PROTOTYPE

Page 17: Beauty of metaprogramming

INHERITANCE

Page 18: Beauty of metaprogramming

MIXIN

Page 19: Beauty of metaprogramming

AFTER MIX(ED) IN…

Page 20: Beauty of metaprogramming

EVALS ARE EVIL

Page 21: Beauty of metaprogramming

instance_eval

Page 22: Beauty of metaprogramming

class_eval

Page 23: Beauty of metaprogramming

METHOD MISSING

Page 24: Beauty of metaprogramming

Q&A

Page 25: Beauty of metaprogramming

THANK YOU