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
860
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
100
Hallo, wir sind die Cyborgs: Implantate, Body-Hacks und Rock ‘n Roll
rin
1
94
Let's go to Mars #rp14
rin
0
250
Angular Directives For The Rest Of Us
rin
1
330
Other Decks in Programming
See All in Programming
ファインディ株式会社におけるMCP活用とサービス開発
starfish719
0
240
print("Hello, World")
eddie
1
510
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
130
Improving my own Ruby thereafter
sisshiki1969
1
160
モバイルアプリからWebへの横展開を加速した話_Claude_Code_実践術.pdf
kazuyasakamoto
0
310
Processing Gem ベースの、2D レトロゲームエンジンの開発
tokujiros
2
120
Design Foundational Data Engineering Observability
sucitw
3
180
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
2
1.9k
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
380
為你自己學 Python - 冷知識篇
eddie
1
340
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
320
testingを眺める
matumoto
1
130
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Writing Fast Ruby
sferik
628
62k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.9k
A better future with KSS
kneath
239
17k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
Why Our Code Smells
bkeepers
PRO
339
57k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
800
We Have a Design System, Now What?
morganepeng
53
7.8k
Docker and Python
trallard
45
3.5k
Testing 201, or: Great Expectations
jmmastey
45
7.6k
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