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
110
Choosing the right technology
xinuc
0
170
A Tale of a Happy Programmer
xinuc
0
130
Rails on Wiradipa - Jakarta.rb Februari 2012 - Hafiz Badrie Lubiz
xinuc
1
150
Why Ruby? - View from business aspect - Jakarta.rb Februari 2012 - Fajrin Rasyid
xinuc
1
340
Other Decks in Programming
See All in Programming
Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | AndroidMakers Paris
prof18
0
160
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
27
8.5k
Elm Form Validation
bkuhlmann
0
520
Site Reliability Engineering for GMO
pyama86
8
1.1k
SIMD Parallel Programming with the Vector API
josepaumard
0
230
業務ツールとして使うPostman
msys75
0
110
Polars入門
daikikatsuragawa
1
180
Tailwind CSSを本気でカスタマイズする方法
fsubal
15
5.5k
PostmanでAPIの動作確認が楽になった話
h455h1
0
180
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
140
Three ways to use AI on Android: The Good, the Bad and the Ugly
marxallski
0
110
Webアプリをできるだけコードを手書きしないで作ってみる
tomokusaba
2
180
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
126
32k
Large-scale JavaScript Application Architecture
addyosmani
504
110k
Into the Great Unknown - MozCon
thekraken
15
1k
In The Pink: A Labor of Love
frogandcode
138
21k
BBQ
matthewcrist
80
8.8k
Code Review Best Practice
trishagee
56
15k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
21
1.9k
The Art of Programming - Codeland 2020
erikaheidi
43
12k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
275
13k
Designing for humans not robots
tammielis
247
25k
The Cult of Friendly URLs
andyhume
74
5.7k
It's Worth the Effort
3n
180
27k
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