Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
This Talk is so Meta
Search
Nugroho Herucahyono
September 26, 2013
Programming
1
130
This Talk is so Meta
Introduction to metaprogramming in ruby
Nugroho Herucahyono
September 26, 2013
Tweet
Share
More Decks by Nugroho Herucahyono
See All by Nugroho Herucahyono
Simply Distributed
xinuc
0
120
Choosing the right technology
xinuc
0
180
A Tale of a Happy Programmer
xinuc
0
150
Rails on Wiradipa - Jakarta.rb Februari 2012 - Hafiz Badrie Lubiz
xinuc
1
160
Why Ruby? - View from business aspect - Jakarta.rb Februari 2012 - Fajrin Rasyid
xinuc
1
350
Other Decks in Programming
See All in Programming
AI時代のソフトウェア開発を考える(2025/07版) / Agentic Software Engineering Findy 2025-07 Edition
twada
PRO
93
31k
Team operations that are not burdened by SRE
kazatohiei
1
320
ニーリーにおけるプロダクトエンジニア
nealle
0
870
Claude Code + Container Use と Cursor で作る ローカル並列開発環境のススメ / ccc local dev
kaelaela
10
6k
AIともっと楽するE2Eテスト
myohei
7
2.8k
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
180
NPOでのDevinの活用
codeforeveryone
0
860
レベル1の開発生産性向上に取り組む − 日々の作業の効率化・自動化を通じた改善活動
kesoji
0
240
おやつのお供はお決まりですか?@WWDC25 Recap -Japan-\(region).swift
shingangan
0
140
チームで開発し事業を加速するための"良い"設計の考え方 @ サポーターズCoLab 2025-07-08
agatan
1
440
PipeCDのプラグイン化で目指すところ
warashi
1
280
10 Costly Database Performance Mistakes (And How To Fix Them)
andyatkinson
0
440
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
980
Thoughts on Productivity
jonyablonski
69
4.7k
Building Applications with DynamoDB
mza
95
6.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
It's Worth the Effort
3n
185
28k
Docker and Python
trallard
44
3.5k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
The Cost Of JavaScript in 2023
addyosmani
51
8.5k
Building an army of robots
kneath
306
45k
Designing Experiences People Love
moore
142
24k
For a Future-Friendly Web
brad_frost
179
9.8k
Transcript
Jakarta.rb 25 September 2013
Xinuc (Ruby) Programmer @xinuc (github, twitter) Works at Bukalapak.com Intro
We're hiring www.bukalapak.com/career Intro
This Talk is so Meta
Metaprogramming, because F*** You, That's why!
Metaprogramming: the writing of computer programs that write or manipulate
other programs (or themselves) as their data, or that do potato potato potato... Metaprogramming
Write Program that writes program (that writes program) (that writes
program) Metaprogramming
- Meet your first Meta - attr_accessor - The basic
- block - ruby class structure - method lookup - eigenclass - The technique - define_method - method_missing - method cache - class_eval - instance_eval - eval Agenda
Meet Your first meta
class Person { private String name; public String getName() {
return this.name; } public void setName(String name) { this.name = name; } } attr_accessor
class Person attr_accessor :name end attr_accessor
class Person def name @name end def name=(name) @name =
name end end attr_accessor
The Basic Some groundwork we need to lay
block
A block is a just chunk of code that can
be called 3.times do puts “hello” end block
A block is just a special form of a proc
def show_me_the_block(&block) block end block vs proc
$ b = show_me_the_block do puts “hello” end => #<Proc:0x00000000d8d600:4>
$ b.call => Hello $ b.class => Proc block vs proc
my_proc = proc { puts “hello” } my_proc.call => hello
3.times &my_proc => hello hello hello block vs proc
A block is a closure because it can access its
environment name = “xinuc” 3.times do puts “hello #{name}” end block & closure
Ruby method lookup
Inside a ruby object klass iv_tbl flags klass iv_tbl flags
m_tbl super object class
Class structure MyClass Object Class super klass klass klass
Class structure Object BasicObject super MyClassA super MyClassB super
Class structure Object BasicObject super MyClassA super MyClassB super MyModule
mixin
eigenclass
o = Object.new o.hello => NoMethodError: undefined method `hello' def
o.hello puts “hello” end o.hello => hello eigen_class
o = Object.new eigen = o.singleton_class #1.9 eigen = class
<< o; self; end eigen_class
Class structure Object BasicObject super MyClassA super eigen_class <virtual> super
MyModule object klass mixin
The Techniques
Define method
# nested def def learn_to_shout puts “learning....” def shout puts
“Wazzzaaaap!!” end end Define method
$ shout => undefined local variable or method `shout' $
learn_to_shout => learning.... $ shout => Wazzzaaaap!! Define method
def learn_to_shout puts “learning....” def shout puts “Wazzzaaaap!!” end end
Define method
def who_are_you puts “I'm batman” def who_are_you puts “I'm superman”
def who_are_you puts “I'm ironman” end end end Define method
def who_are_you puts “I'm batman” def who_are_you puts “I'm superman”
def who_are_you puts “I'm ironman” end end end Hypocrite method
def who_are_you puts “I'm batman” def who_are_you puts “I'm superman”
def who_are_you puts “I'm ironman” end end end Yo dawg method
define_method
define_method method takes a method name and a block, then
creates a method with block as the method body define_method :name do |args| # method body end define_method method
def attr_accessor(*attrs) attrs.each do |attr| define_method attr do instance_variable_get “@#{attr}”
end define_method “#{attr}=” do |val| instance_variable_set “@#{attr}”, val end end end attr_accessor
method_missing
class Person def method_missing(name, *args, &block) puts “method #{name}” end
end p = Person.new p.omg_lol => method omg_lol method_missing
require 'builder' xml = Builder::XmlMarkup.new xml.coder do xml.name 'xinuc' xml.language
'Ruby' end method_missing
Class structure Object BasicObject super MyClass super eigen_class <virtual> super
MyModule my_object klass mixin
Lazy method definition
class Person def method_missing(name, *args, &block) self.class.class_eval do define_method(name) do
puts ”method #{defined} defined” end end self.send name, *args end end Lazy method definition
p = Person.new p.respond_to? :hello => false p.hello => method
hello defined p.respond_to? :hello => true Lazy method definition
class User < ActiveRecord::Base end User.find_by_username “xinuc” Lazy method definition
class_eval
class Person end Person.class_eval do def hello puts “hello” end
end p = Person.new p.hello class_eval
instance_eval
class Person end p = Person.new p.instance_eval do def hello
puts “hello” end end instance_eval
class Person end p = Person.new p.singleton_class.class_eval do def hello
puts “hello” end end p.hello instance_eval
instance_eval change the “self” inside a block s = “Hello”
s.instance_eval do puts self.upcase end instance_eval
# sunspot class User < ActiveRecord::Base # method text &
integer # not defined here searchable do # but we can call method text & integer here text :name integer :age end end instance_eval
Here comes the devil
Class.new Create classes dynamically
1000.times do Class.new do end end Class.new
“The evil” eval
eval <<-EOF def do_evil puts “do evil things” end EOF
do_evil eval
Warning!
Metaprogramming is a chainsaw warning
You don't slice your bread with a chainsaw! warning
Unless you're a bear with chainsaw hands warning
Thank you