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
YJIT Makes Rails 1.7x faster / RubyKaigi 2024
k0kubun
7
13k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.6k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
2k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
11k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
420
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
780
Ruby 3.0 JIT on Rails
k0kubun
9
9.2k
Other Decks in Programming
See All in Programming
Scaling your build logic
antalmonori
1
130
Rubyでつくるパケットキャプチャツール
ydah
0
260
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
230
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
3
940
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
1.1k
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
8
2k
.NETでOBS Studio操作してみたけど…… / Operating OBS Studio by .NET
skasweb
0
130
Beyond ORM
77web
11
1.6k
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
250
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
18
3k
Compose でデザインと実装の差異を減らすための取り組み
oidy
1
220
AWSマネコンに複数のアカウントで入れるようになりました
yuhta28
2
140
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Site-Speed That Sticks
csswizardry
3
290
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
6
210
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.5k
4 Signs Your Business is Dying
shpigford
182
22k
A better future with KSS
kneath
238
17k
Six Lessons from altMBA
skipperchong
27
3.6k
Designing for Performance
lara
604
68k
YesSQL, Process and Tooling at Scale
rocio
170
14k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
510
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
113
50k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
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ͰࢦఆͰ͖Δͷϝιουݺͼग़͠ͷΈ