Slide 1

Slide 1 text

How is ! magic ! formed

Slide 2

Slide 2 text

Rin! @rinpaku @bitcrowd

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

WHY?

Slide 5

Slide 5 text

TIL: There’s bacon in! the Ruby source code

Slide 6

Slide 6 text

What happens when Ruby runs my code?! ! What is an Object?

Slide 7

Slide 7 text

What happens ! when Ruby ! runs my code?

Slide 8

Slide 8 text

Ruby code 9.times do puts "Ente" end

Slide 9

Slide 9 text

Tokens 9 . t i m e s …

Slide 10

Slide 10 text

9 . times do puts "" Tokens 9 . t i m e s … integer identifier identifier period keyword strin

Slide 11

Slide 11 text

Syntax Tree do_block period method_add_block integer! 9 identifier! times call command identifier! puts …

Slide 12

Slide 12 text

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 @

Slide 13

Slide 13 text

I MIGHT! HAVE MADE THIS ALL UP

Slide 14

Slide 14 text

Tokenizing Ripper.lex(your_code)

Slide 15

Slide 15 text

parse.c

Slide 16

Slide 16 text

with Ripper: Ripper.sexp(your_code) ! Parsing

Slide 17

Slide 17 text

with Ripper: Ripper.sexp(your_code) parser debug information: ruby -y example.rb Parsing

Slide 18

Slide 18 text

RubyVM::InstructionSequence. compile(code).disasm Compiling

Slide 19

Slide 19 text

What ! is an! Object?

Slide 20

Slide 20 text

struct meetup { char title[120]; time_t starts_at; bool has_talks; }; Structs

Slide 21

Slide 21 text

union location { char address[120]; struct position { int latitude; int longitude; } }; Union

Slide 22

Slide 22 text

struct meetup { char title[120]; time_t starts_at; bool has_talks; union { char address[120]; struct position { int latitude; int longitude; } } };

Slide 23

Slide 23 text

Let’s look at some source code

Slide 24

Slide 24 text

RObject struct RObject { struct RBasic basic; … };

Slide 25

Slide 25 text

RBasic struct RBasic { VALUE flags; const VALUE klass; };

Slide 26

Slide 26 text

RObject struct RObject { struct RBasic basic; struct { long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } heap; }; simplified

Slide 27

Slide 27 text

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; };

Slide 28

Slide 28 text

RFloat struct RFloat { struct RBasic basic; double float_value; };

Slide 29

Slide 29 text

- 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

Slide 30

Slide 30 text

WAIT! Where’s the bacon?

Slide 31

Slide 31 text

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