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
How is magic formed
Search
Rin Raeuber
July 03, 2014
Programming
0
820
How is magic formed
Short excursion into Ruby Internals, Ruby Usergroup Berlin, June 3rd 2014
Rin Raeuber
July 03, 2014
Tweet
Share
More Decks by Rin Raeuber
See All by Rin Raeuber
Das Internet of Things mit dem ESP2866
rin
0
170
Let's create a game with Ruby
rin
1
97
Hallo, wir sind die Cyborgs: Implantate, Body-Hacks und Rock ‘n Roll
rin
1
91
Let's go to Mars #rp14
rin
0
240
Angular Directives For The Rest Of Us
rin
1
310
Other Decks in Programming
See All in Programming
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
400
Sidekiqで実現する 長時間非同期処理の中断と再開 / Pausing and Resuming Long-Running Asynchronous Jobs with Sidekiq
hypermkt
6
2.7k
gopls を改造したら開発生産性が高まった
satorunooshie
8
240
Hotwire or React? ~Reactの録画機能をHotwireに置き換えて得られた知見~ / hotwire_or_react
harunatsujita
9
4.1k
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
Honoの来た道とこれから
yusukebe
19
3k
JaSST 24 九州:ワークショップ(は除く)実践!マインドマップを活用したソフトウェアテスト+活用事例
satohiroyuki
0
270
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.4k
Pinia Colada が実現するスマートな非同期処理
naokihaba
2
160
Macとオーディオ再生 2024/11/02
yusukeito
0
210
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.6k
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Navigating Team Friction
lara
183
14k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
GraphQLの誤解/rethinking-graphql
sonatard
66
10k
A better future with KSS
kneath
238
17k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
228
52k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Gamification - CAS2011
davidbonilla
80
5k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
What's in a price? How to price your products and services
michaelherold
243
12k
Transcript
How is ! magic ! formed
Rin! @rinpaku @bitcrowd
None
WHY?
TIL: There’s bacon in! the Ruby source code
What happens when Ruby runs my code?! ! What is
an Object?
What happens ! when Ruby ! runs my code?
Ruby code 9.times do puts "Ente" end
Tokens 9 . t i m e s …
9 . times do puts "" Tokens 9 . t
i m e s … integer identifier identifier period keyword strin
Syntax Tree do_block period method_add_block integer! 9 identifier! times call
command identifier! puts …
YARV instructions “Yet Another Ruby Virtual Machine” == catch table
| catch type: break st: 0002 ed: 0006 sp: 0000 cont: 0006 |-------------------------------------------------------------- 0000 trace 1 0002 putobject 9 0004 send <callinfo!mid:times, argc:0, block:block 0006 leave == disasm: <RubyVM::InstructionSequence:block in <compiled>@<co == catch table | catch type: redo st: 0000 ed: 0009 sp: 0000 cont: 0000 | catch type: next st: 0000 ed: 0009 sp: 0000 cont: 0009 |-------------------------------------------------------------- 0000 trace 256 0002 trace 1 0004 putself 0005 putstring "Ente"
I MIGHT! HAVE MADE THIS ALL UP
Tokenizing Ripper.lex(your_code)
parse.c
with Ripper: Ripper.sexp(your_code) ! Parsing
with Ripper: Ripper.sexp(your_code) parser debug information: ruby -y example.rb Parsing
RubyVM::InstructionSequence. compile(code).disasm Compiling
What ! is an! Object?
struct meetup { char title[120]; time_t starts_at; bool has_talks; };
Structs
union location { char address[120]; struct position { int latitude;
int longitude; } }; Union
struct meetup { char title[120]; time_t starts_at; bool has_talks; union
{ char address[120]; struct position { int latitude; int longitude; } } };
Let’s look at some source code
RObject struct RObject { struct RBasic basic; … };
RBasic struct RBasic { VALUE flags; const VALUE klass; };
RObject struct RObject { struct RBasic basic; struct { long
numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } heap; }; simplified
RObject struct RObject { struct RBasic basic; union { struct
{ long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } heap; VALUE ary[ROBJECT_EMBED_LEN_MAX]; } as; };
RFloat struct RFloat { struct RBasic basic; double float_value; };
- Reading C is not that bad.* *(If you
hate it, you can still look at the Standard Library or Rubinius.)! ! - Check out the Ruby source code and give a talk about it. ;) Things to take home
WAIT! Where’s the bacon?
Never create Ruby Strings longer than 23 characters ! Ruby
under a microscope (the book) ! The Ruby Hacking Guide (Warning: Ruby 1.7.3!) ! A Tour of the Ruby MRI Source Code with Pat Shaughnessy Some Links