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
pp and reflect
Search
Takashi Kokubun
June 21, 2015
Programming
6
11k
pp and reflect
Go Conference 2015 summer
Takashi Kokubun
June 21, 2015
Tweet
Share
More Decks by Takashi Kokubun
See All by Takashi Kokubun
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
2
2.1k
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
14k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.9k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2.1k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
450
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.9k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
810
Other Decks in Programming
See All in Programming
Designing Your Organization's Test Pyramid ( #scrumniigata )
teyamagu
PRO
5
1.4k
複雑なフォームの jotai 設計 / Designing jotai(state) for Complex Forms #layerx_frontend
izumin5210
6
1.5k
最速Green Tea 🍵 Garbage Collector
kuro_kurorrr
1
120
ウォンテッドリーの「ココロオドル」モバイル開発 / Wantedly's "kokoro odoru" mobile development
kubode
2
550
LRパーサーはいいぞ
ydah
7
1.3k
By the way Google Cloud Next 2025に行ってみてどうだった
ymd65536
0
130
データベースの技術選定を突き詰める ~複数事例から考える最適なデータベースの選び方~
nnaka2992
0
620
20250426 GDGoC 合同新歓 - GDGoC のススメ
getty708
0
110
API for docs
soutaro
4
1.7k
UMAPをざっくりと理解 / Overview of UMAP
kaityo256
PRO
3
1.5k
生成AI時代のフルスタック開発
kenn
4
500
AIコーディングエージェントを 「使いこなす」ための実践知と現在地 in ログラス / How to Use AI Coding Agent in Loglass
rkaga
4
1.3k
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
40
7.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
Why You Should Never Use an ORM
jnunemaker
PRO
56
9.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
Six Lessons from altMBA
skipperchong
28
3.8k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
24
2.7k
Statistics for Hackers
jakevdp
799
220k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
Transcript
pp and reflect Go Conference 2015 summer Takashi Kokubun
Who is k0kubun? • Twitter, GitHub: @k0kubun • Cookpad •
Rubyist
k0kubun/pp Colored Pretty Printer https://github.com/k0kubun/pp
k0kubun/pp • Prettily print any complex data structures • Simple
APIs like “fmt” import "github.com/k0kubun/pp" for _, tweet := range tweets { pp.Println(tweet) }
k0kubun/pp • Prettily print any complex data structures • Simple
APIs like “fmt”
k0kubun/pp ͷෆศͳॴ • ᶃ import͢ΔURL͕ࢥ͍ग़ͤͳ͍ • ᶄ import͢͠ͷ͕ΊΜͲ͍͘͞
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ Ϣʔβʔͷෆຬᶃ
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ import "github.com/◦◦◦/pp" ↑ ͜ͷਓ୭͚ͩͬ…
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ ppͷcontributorͷํ
import͢ΔURL͕ࢥ͍ग़ͤͳ͍ ձࣾͷઌഐ ͍͍ͪͪιʔείʔυʹ k0kubun͞Μͷ໊લॻ͖ͨ͘ͳ͍Ͱ͢Ͷ…
go-pp/ppͱ͍͏forkΛimport import “github.com/go-pp/pp" or import “gopkg.in/pp.v2” ղܾํ๏ɿ
import͢͠ͷ͕ΊΜͲ͍͘͞ Ϣʔβʔͷෆຬᶄ
import͢͠ͷ͕ΊΜͲ͍͘͞ import "github.com/k0kubun/pp" ↑ pp.Println() Λॻ͍ͨΓফͨ͠Γ͢ ΔͨͼʹՃ͢͠ඞཁ͕͋Δ
ؔΛϥοϓͯ͠gitignore import p "github.com/k0kubun/pp" func pp(a ...interface{}) { p.Println(a...) }
• ʮ,*ʯΛgitignoreͯ͠ΔͳΒʮ,.goʯͱ͔ • ͬͯͳͯ͘importফ͞ͳͯ͘ࡁΉ ղܾํ๏ɿ
package “reflect” Standard library for reflection k0kubun/pp is powered by
“reflect”
What’s the usage of reflect? • ͷϝλใΛಡΉ • ppͩͱstructͷશfieldྻڍ •
encoding/jsonͩͱstructͷfieldͷλά • ϝλϓϩάϥϛϯά
ϝλϓϩάϥϛϯάͷಛ • εϚʔτͳॻ͖ํͰίʔυͷߦΛݮΒͤΔ • ॻָ͍͍ͯͯ͠ • ੩తղੳ͕͘͠ɺಡΈʹ͘͘ͳΔ • झຯҎ֎ͰΘͳ͍ͷ͕ແ
reflectͰͰ͖Δͷ࣍ͷ͏ͪͲΕʁ Go ϝλϓϩάϥϛϯάΫΠζ
reflectͰͰ͖Δͷ࣍ͷ͏ͪͲΕʁ ૢ࡞ ճ ؔఆٛ ؔݺͼग़͠ ϝιουఆٛ ϝιουݺͼग़͠ ҙͷܕͷOFX ˕ɿstringͰ໊લΛࢦఆՄɹ˓ɿreflect.Value͔ΒՄೳɹ×ɿແཧ ˕ɺ˓ɺ×
ώϯτ: Rubyશ෦˕
reflectͰͰ͖Δͷ࣍ͷ͏ͪͲΕʁ ૢ࡞ ճ ؔఆٛ ˓ ؔݺͼग़͠ ˓ ϝιουఆٛ º ϝιουݺͼग़͠
˕ ҙͷܕͷOFX ˓ ˕ɿstringͰ໊લΛࢦఆՄɹ˓ɿreflect.Value͔ΒՄೳɹ×ɿແཧ ϝλϓϩՄ
reflectʹΑΔؔఆٛ • ҙͷγάωνϟͷؔΛ࡞ΕΔ var f func(int, int) int v :=
reflect.MakeFunc( reflect.TypeOf(f), func(in []reflect.Value) []reflect.Value { return []reflect.Value{in[0]} }, ) reflect.ValueOf(&f).Elem().Set(v)
reflectʹΑΔؔݺͼग़͠ • ҙͷγάωνϟͷؔΛݺͼग़ͤΔ f := func(a int, b int) int
{ return a + b } v := reflect.ValueOf(f) in := []reflect.Value{} for i := 0; i < v.Type().NumIn(); i++ { in = append(in, reflect.ValueOf(1)) } v.Call(in)
reflectʹΑΔϝιουఆٛ • ී௨ʹແཧ
reflectʹΑΔϝιουݺͼग़͠ • stringͰ໊લΛࢦఆͯ͠ϝιουΛݺͼग़ͤΔ type Hoge struct{} func (h Hoge) Foo()
{ print("foo") } func (h Hoge) Bar() { print("bar") } h := new(Hoge) for _, name := range []string{"Foo", "Bar"} { v := reflect.ValueOf(h) v.MethodByName(name).Call([]reflect.Value{}) }
reflectʹΑΔnew • ҙͷܕͷΛॳظԽͨ͠ͷΛ࡞ΕΔ t := reflect.TypeOf(a) reflect.New(t)
·ͱΊ • k0kubun/ppͬͯԼ͍͞ • pp.Println() Λϥοϓͯ͠gitignore͓ͯ͘͠ • reflect͏ͱϝλϓϩͰ͖Δ • stringͰࢦఆͰ͖Δͷϝιουݺͼग़͠ͷΈ