Last active
August 29, 2015 14:22
-
-
Save AllPurposeName/ed9976ce4d64d44928e5 to your computer and use it in GitHub Desktop.
Metaprogramming is meta-cool!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Metaprogramming is Metacool! | |
* Introduction | |
-- Brief explanation on what metaprogramming is and why it's a cornerstone of OO Porgramming languages | |
-- We'll open up String and Fixnum to replace/add functionality respectively. | |
1. Explain concept of opening a class and changing/adding methods. | |
* Monkey Patching | |
--Implementing it the easy way | |
--Implementing it the right way | |
--Why not to implement it at all | |
2. Explain experiment of opening String to redefine nil? to pass true on empty strings | |
3. Do it with a simple class String; def nil?; if self == ""; return true; end | |
4. Namespacing with a Module and where to put said Module | |
5. Show how this can mess things up. Example: | |
instructor_names = ["Rachel Warbelow", "Josh Mejia", ""] we want [-1].to be(nil) | |
vs full_name = ["DJ", "", "Greenfield] we want [1].to be.ok | |
p.s. expect(DSLs.really).to be(suck) | |
--Segue-- Metaprogramming usually violates these two principles: | |
Principle of Least Surprise, as seen in String example, | |
Single Responsibility Principle, as I'll show in Fixnum example | |
* Refinements | |
--[Why seperate knob?](https://www.youtube.com/watch?v=JRGgTTxdwsE) | |
--How to implement it | |
--Problems? | |
1. Create a refinement for Fixnum to implement 1.days.ago functionality. | |
2. Explain it's different because it scopes the changes. | |
3. Downsides: Performance issues, new--not generally accepted and new syntax, monkey patching is easy and 'works' why fix it? | |
* Takeaway | |
--Monkey Patching and most metaprogramming is ambiguous, hard to test, and often extremely misleading (think DSL). Big applications (Rails) often rely on metaprogramming so much it's 'magic' is hard to parse and deal with. This hurts the community as Ruby has very specific goals which metaprogramming unfailingly undermines. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment