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

Viewers also liked

Studi kasus di rz batam
Studi kasus di rz batamStudi kasus di rz batam
Studi kasus di rz batamHamdan Aly
 
Iq telecom
Iq telecomIq telecom
Iq telecom
Iqtelecom Voip
 
Use of LCA tools in the early stages of a research project
Use of LCA tools in the early stages of a research projectUse of LCA tools in the early stages of a research project
Use of LCA tools in the early stages of a research project
Olivier Talon
 
Vb net aplikasi surat izin mengemudi
Vb net aplikasi surat izin mengemudiVb net aplikasi surat izin mengemudi
Vb net aplikasi surat izin mengemudiHamdan Aly
 
жумахан+жамиля+инкубатор+клиенты4,1
жумахан+жамиля+инкубатор+клиенты4,1жумахан+жамиля+инкубатор+клиенты4,1
жумахан+жамиля+инкубатор+клиенты4,1
Жумахан Жамиля
 
Priyanka Resume
Priyanka ResumePriyanka Resume
Priyanka Resume
Priyanka Wadhwani
 
Carreira_Portfolio_11-2016
Carreira_Portfolio_11-2016Carreira_Portfolio_11-2016
Carreira_Portfolio_11-2016Jeff Carreira
 
IEEE ELECTRIFICATION MAGAZINE
IEEE ELECTRIFICATION MAGAZINEIEEE ELECTRIFICATION MAGAZINE
IEEE ELECTRIFICATION MAGAZINECris Vinueza
 
10 Digital marketing terms you need to know
10 Digital marketing terms you need to know10 Digital marketing terms you need to know
10 Digital marketing terms you need to know
Astha Kalbag
 

Viewers also liked (9)

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

Similar to Lightning Talk on Metaprogramming in Ruby

Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
rivierarb
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
Alex Koppel
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
Tushar Pal
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
Iq rails
Iq railsIq rails
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
Henry S
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
Sumanth krishna
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
Gonçalo Silva
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
Reuven Lerner
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council Pune
Pankaj kshirsagar
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
bobmcwhirter
 
مقایسه و بررسی چهارچوب ریلز
مقایسه و بررسی چهارچوب ریلزمقایسه و بررسی چهارچوب ریلز
مقایسه و بررسی چهارچوب ریلز
railsbootcamp
 
Language supports it
Language supports itLanguage supports it
Language supports it
Niranjan Paranjape
 
Ruby thread safety first
Ruby thread safety firstRuby thread safety first
Ruby thread safety first
Emily Stolfo
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
Cory Foy
 
OOPs Concepts.pptx
OOPs Concepts.pptxOOPs Concepts.pptx
OOPs Concepts.pptx
cheapPerson
 
JavaScripts internals #1
JavaScripts internals #1JavaScripts internals #1
JavaScripts internals #1
Martin Pernica
 

Similar to Lightning Talk on Metaprogramming in Ruby (20)

Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
 
Iq rails
Iq railsIq rails
Iq rails
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council Pune
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
مقایسه و بررسی چهارچوب ریلز
مقایسه و بررسی چهارچوب ریلزمقایسه و بررسی چهارچوب ریلز
مقایسه و بررسی چهارچوب ریلز
 
Language supports it
Language supports itLanguage supports it
Language supports it
 
Web20
Web20Web20
Web20
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Ruby thread safety first
Ruby thread safety firstRuby thread safety first
Ruby thread safety first
 
Ruby for C# Developers
Ruby for C# DevelopersRuby for C# Developers
Ruby for C# Developers
 
OOPs Concepts.pptx
OOPs Concepts.pptxOOPs Concepts.pptx
OOPs Concepts.pptx
 
JavaScripts internals #1
JavaScripts internals #1JavaScripts internals #1
JavaScripts internals #1
 

Recently uploaded

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 

Recently uploaded (20)

Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 

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