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
12k
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9.4k
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
410
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.8k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
760
Ruby 3.0 JIT on Rails
k0kubun
9
9.1k
Other Decks in Programming
See All in Programming
受け取る人から提供する人になるということ
little_rubyist
0
230
Remix on Hono on Cloudflare Workers
yusukebe
1
280
Jakarta EE meets AI
ivargrimstad
0
140
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
660
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Featured
See All Featured
Designing for Performance
lara
604
68k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
How STYLIGHT went responsive
nonsquared
95
5.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Statistics for Hackers
jakevdp
796
220k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Speed Design
sergeychernyshev
24
610
Site-Speed That Sticks
csswizardry
0
23
Building a Scalable Design System with Sketch
lauravandoore
459
33k
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ͰࢦఆͰ͖Δͷϝιουݺͼग़͠ͷΈ