SlideShare a Scribd company logo
Metaprogramming
what’s up with that
Yo, Dawg - I heard you like programming
so I got you some meta
so you can program your programs
Using Open Classes is Meta
class Integer
def hearts
self.times do
print "u2764 "
end
end
end
irb(main):003:0> require "./meta.rb"
=> true
irb(main):004:0> 3.hearts
❤ ❤ ❤ => 3
irb(main):005:0> 1.hearts
❤ => 1
Objects and Classes
Object stores only
variables and a
reference to the Class
Class stores only
methods (both instance
and class methods)

class = :Animal
@says = “bark”

def speak
	 puts @says
end
A class is an object!
you can change variables on an object
you can change methods on a class
in ruby, you can do this whenever you want
if your code changes other code, especially
when running, that’s metaprogramming
Text

Can be dangerous!
But ruby 2.0 makes it safer
Refine is safer (ruby 2)
module MadHacks
refine Integer do
def hearts
self.times do
print "u2764 "
end
end
end
end

!

irb(main):001:0> require "./meta.rb"
=> true
irb(main):002:0> 1.hearts
NoMethodError: undefined method `hearts' for 1:Fixnum
from (irb):2
from /Users/mindlace/.rbenv/versions/2.0.0-p247/bin/irb:12:in `<main>'
irb(main):003:0> using MadHacks
=> main
irb(main):004:0> 1.hearts
❤ => 1
Lots of other techniques
Dynamic methods (add methods when they are named)
Method missing (respond to any method name)
And more!
Ruby makes it very easy to metaprogram
With power comes responsibility
Questions?

More Related Content

PPTX
Lec 1.5 Object Oriented Programming
PPTX
Chapter 6 java
PDF
JavaScript 101 - Class 2
PPTX
Dictionaries in Python
PPTX
Lec 1.8 Object Oriented Programming
PPTX
IT SCope Fool
PDF
ShopCast-v4.0-Payment
PPTX
html5
Lec 1.5 Object Oriented Programming
Chapter 6 java
JavaScript 101 - Class 2
Dictionaries in Python
Lec 1.8 Object Oriented Programming
IT SCope Fool
ShopCast-v4.0-Payment
html5

Viewers also liked (9)

DOCX
Studi kasus di rz batam
PPTX
Iq telecom
PPT
Use of LCA tools in the early stages of a research project
DOCX
Vb net aplikasi surat izin mengemudi
PPT
жумахан+жамиля+инкубатор+клиенты4,1
DOCX
Priyanka Resume
PDF
Carreira_Portfolio_11-2016
DOCX
IEEE ELECTRIFICATION MAGAZINE
PPTX
10 Digital marketing terms you need to know
Studi kasus di rz batam
Iq telecom
Use of LCA tools in the early stages of a research project
Vb net aplikasi surat izin mengemudi
жумахан+жамиля+инкубатор+клиенты4,1
Priyanka Resume
Carreira_Portfolio_11-2016
IEEE ELECTRIFICATION MAGAZINE
10 Digital marketing terms you need to know
Ad

Similar to Lightning Talk on Metaprogramming in Ruby (20)

PDF
Ruby seen from a C# developer
PDF
Ruby seen by a C# developer
PDF
Metaprogramming ruby
PDF
Ruby — An introduction
PDF
Ruby Metaprogramming 08
PDF
Ruby Metaprogramming - OSCON 2008
PDF
Ruby Presentation
PDF
Metaprogramming
KEY
Introduction to Ruby
PPTX
The Black Magic of Ruby Metaprogramming
PDF
Metaprogramming in Ruby
PDF
Devoxx%202008%20Tutorial
PDF
Devoxx%202008%20Tutorial
PPTX
Day 1 - Intro to Ruby
PPTX
Nairuby metaprogramming with ruby
KEY
KEY
Metaprogramming Primer (Part 1)
ODP
PPTX
The Metasploit Way TO USE IT IN DA PPT.pptx
KEY
Ruby on Rails Training - Module 1
Ruby seen from a C# developer
Ruby seen by a C# developer
Metaprogramming ruby
Ruby — An introduction
Ruby Metaprogramming 08
Ruby Metaprogramming - OSCON 2008
Ruby Presentation
Metaprogramming
Introduction to Ruby
The Black Magic of Ruby Metaprogramming
Metaprogramming in Ruby
Devoxx%202008%20Tutorial
Devoxx%202008%20Tutorial
Day 1 - Intro to Ruby
Nairuby metaprogramming with ruby
Metaprogramming Primer (Part 1)
The Metasploit Way TO USE IT IN DA PPT.pptx
Ruby on Rails Training - Module 1
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPT
Teaching material agriculture food technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Big Data Technologies - Introduction.pptx
Teaching material agriculture food technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectroscopy.pptx food analysis technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Lightning Talk on Metaprogramming in Ruby

  • 2. Yo, Dawg - I heard you like programming so I got you some meta so you can program your programs
  • 3. Using Open Classes is Meta class Integer def hearts self.times do print "u2764 " end end end irb(main):003:0> require "./meta.rb" => true irb(main):004:0> 3.hearts ❤ ❤ ❤ => 3 irb(main):005:0> 1.hearts ❤ => 1
  • 4. Objects and Classes Object stores only variables and a reference to the Class Class stores only methods (both instance and class methods) class = :Animal @says = “bark” def speak puts @says end
  • 5. A class is an object! you can change variables on an object you can change methods on a class in ruby, you can do this whenever you want if your code changes other code, especially when running, that’s metaprogramming
  • 6. Text Can be dangerous! But ruby 2.0 makes it safer
  • 7. Refine is safer (ruby 2) module MadHacks refine Integer do def hearts self.times do print "u2764 " end end end end ! irb(main):001:0> require "./meta.rb" => true irb(main):002:0> 1.hearts NoMethodError: undefined method `hearts' for 1:Fixnum from (irb):2 from /Users/mindlace/.rbenv/versions/2.0.0-p247/bin/irb:12:in `<main>' irb(main):003:0> using MadHacks => main irb(main):004:0> 1.hearts ❤ => 1
  • 8. Lots of other techniques Dynamic methods (add methods when they are named) Method missing (respond to any method name) And more! Ruby makes it very easy to metaprogram With power comes responsibility