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
4
570
Ruby JIT Hacking Guide / RubyKaigi 2023
k0kubun
2
9k
YJIT: Dive into Ruby's JIT compiler written in Rust / Rust.Tokyo 2022
k0kubun
1
1.8k
Towards Ruby 4 JIT / RubyKaigi 2022
k0kubun
3
10k
Optimizing Production Performance with MRI JIT / RubyConf 2021
k0kubun
1
350
Why Ruby's JIT was slow / RubyKaigi Takeout 2021
k0kubun
3
1.7k
数時間かかる週一リリースを毎日何度も爆速でできるようにするまで / CI/CD Conference 2021
k0kubun
21
14k
Ruby 3 JIT's roadmap / RubyConf China 2020
k0kubun
0
670
Ruby 3.0 JIT on Rails
k0kubun
9
8.9k
Other Decks in Programming
See All in Programming
Go製Webアプリケーションのエラーとの向き合い方大全、あるいはやっぱりスタックトレース欲しいやん / Kyoto.go #50
utgwkk
6
2k
2024 コーディング研修
ckazu
2
680
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
1
140
TypeScriptで使いやすいOpenAPIの書き方
yukimochi_dwango
1
970
Powerfully Typed TypeScript
euxn23
4
1.8k
CQRS meets modern Java
simas
PRO
2
480
Good first issues of TypeProf
mame
4
940
WebGLで始める コンピュータグラフィックス入門
heller77
0
390
The grand strategy of Ruby Parser
yui_knk
5
430
Dev ContainersとTestcontainers
bells17
3
100
[RubyKaigi 2024] Ruby Mixology 101: adding shots of PHP, Elixir, and more
palkan
0
140
一文字エイリアスのすすめ
fujimura
0
200
Featured
See All Featured
Done Done
chrislema
178
15k
The World Runs on Bad Software
bkeepers
PRO
61
6.8k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
Design by the Numbers
sachag
274
18k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Faster Mobile Websites
deanohume
300
30k
Music & Morning Musume
bryan
41
5.6k
Docker and Python
trallard
35
2.7k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
26
2.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
YesSQL, Process and Tooling at Scale
rocio
165
13k
A better future with KSS
kneath
231
16k
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ͰࢦఆͰ͖Δͷϝιουݺͼग़͠ͷΈ