SlideShare a Scribd company logo
1 of 92
Download to read offline
LEARNING RUBY BY
READING THE SOURCE
tw:@burkelibbey / gh:@burke
THESIS:
The best way to learn a piece of infrastructure is to learn about
how it’s implemented.
So let’s dig in to ruby’s source!
TOPICS
• Basic

Object Structure

• Class

inheritance

• Singleton
• Module
• MRI

classes

inheritance

Source spelunking
BASIC OBJECT STRUCTURE
Every object has an RBasic
struct RBasic {
VALUE flags;
VALUE klass;
}
flags stores information like whether the object is
frozen, tainted, etc.
struct RBasic {
VALUE flags;
VALUE klass;
}

It’s mostly internal stuff that you don’t think about
very often.
klass is a pointer to the class of the object
struct RBasic {
VALUE flags;
VALUE klass;
}

(or singleton class, which we’ll talk about later)
...but what’s a VALUE?
struct RBasic {
VALUE flags;
VALUE klass;
}
VALUE is basically used as a void pointer.

typedef uintptr_t VALUE;

It can point to any ruby value.
You should interpret “VALUE” as:
“a (pointer to a) ruby object”
This is a Float.
struct RFloat {
struct RBasic basic;
double float_value;
}
Every type of object, including Float, has an RBasic.
struct RFloat {
struct RBasic basic;
double float_value;
}
And then, after the RBasic, type-specific info.
struct RFloat {
struct RBasic basic;
double float_value;
}
Ruby has quite a few types.
Each of them has their own
type-specific data fields.
But given a ‘VALUE’, we don’t
know which type we have.
How does ruby know?
Every object has an RBasic
struct RBasic {
VALUE flags;
VALUE klass;
}

And the object type is stored inside flags.
Given an object of unknown
type...
VALUE a

struct αѕgє‫נ‬qqωσ {
struct RBasic basic;
ιηт ‫נ‬ѕƒкq; // ???
ƒ‫נ‬є σтн‫¢נ‬є; // ???
}

We can extract the type from ‘basic’, which is
guaranteed to be the first struct member.
e.g. if the type is T_STRING,
struct RString {
struct RBasic basic;
union {
struct {
long len;
...

then we know it’s a `struct RString`.
Every* type corresponds to a
struct type, which ALWAYS
has an RBasic as the first
struct member.

* exceptions for immediate values
There are custom types for
primitives, mostly to make
them faster.
The special-case primitive
types aren’t particularly
surprising or interesting.
T_STRING => RString
RBasic, string data, length.
T_ARRAY => RArray
RBasic, array data, length.
T_HASH => RHash
RBasic, hashtable.
...and so on.
T_OBJECT (struct RObject)
is pretty interesting.
It’s what’s used for instances
of any classes you define, or
most of the standard library.
TL;DR: Instance Variables.
struct RObject {
struct RBasic basic;
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
}

This makes sense; an instance of a class has its own
data, and nothing else.
It stores the number of instance variables
struct RObject {
struct RBasic basic;
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
}
struct RObject {
struct RBasic basic;
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
}

And a pointer to a hashtable containing the
instance variables
This is a shortcut to the class variables of the
object’s class.
struct RObject {
struct RBasic basic;
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
}

You could get the same result by looking it up on
basic.klass (coming up right away)
This definition is actually slightly simplified. I
omitted another performance optimization for
readability.
struct RObject {
struct RBasic basic;
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
}

Go read the full one after this talk if you’re so
inclined!
Class and Module types
Classes have instance variables (ivars),
class variables (cvars), methods, and a superclass.
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
}
This is where the methods live.
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
}

st_table is the hashtable implementation ruby
uses internally.
Class variables live in iv_index_tbl.
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
}
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
}
struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
}
typedef struct rb_classext_struct 
rb_classext_t;
The superclass, instance variables, and constants
defined inside the class.
struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
}
It ends up looking kinda like:
struct RClass {
struct RBasic basic;
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
}

...though this isn’t really valid because rb_classext_t
is referred to by a pointer.
So classes have:
struct RClass {
struct RBasic basic;
VALUE super;
(st) *iv_tbl;
(st) *const_tbl;
(st) *m_tbl;
(st) *iv_index_tbl;
}

*
*
*
*
*
*

RBasic
superclass
instance vars.
constants
methods
class vars.
Modules
Same underlying type (struct RClass) as a class

#define RCLASS(obj) (R_CAST(RClass)(obj))
#define RMODULE(obj) RCLASS(obj)

...just has different handling in a few code paths.
Immediate values
Sort of complicated.
For an integer N:
The fixnum representation is:
2N + 1
enum ruby_special_consts {
RUBY_Qfalse = 0,
RUBY_Qtrue = 2,
RUBY_Qnil
= 4,
RUBY_Qundef = 6,
RUBY_IMMEDIATE_MASK
RUBY_FIXNUM_FLAG
RUBY_SYMBOL_FLAG
RUBY_SPECIAL_SHIFT
};

=
=
=
=

0x03,
0x01,
0x0e,
8
enum ruby_special_consts {
RUBY_Qfalse = 0,
RUBY_Qtrue = 2,
RUBY_Qnil
= 4,
RUBY_Qundef = 6,
RUBY_IMMEDIATE_MASK = 0x03,
RUBY_FIXNUM_FLAG
= 0x01,
A RUBY_SYMBOL_FLAG a big integer, with a
pointer is basically just
= 0x0e,
number referring to a memory
RUBY_SPECIAL_SHIFT = 8 address.
};
enum ruby_special_consts {
RUBY_Qfalse = 0,
RUBY_Qtrue = 2,
RUBY_Qnil
= 4,
RUBY_Qundef = 6,
Remember how a VALUE is mostly a pointer?
RUBY_IMMEDIATE_MASK = 0x03,
These tiny addresses are in the0x01, space
RUBY_FIXNUM_FLAG
= kernel
in a process image, which means they’re
RUBY_SYMBOL_FLAG
= 0x0e,
unaddressable.
RUBY_SPECIAL_SHIFT = 8
};
So ruby uses them to refer to special values.
enum ruby_special_consts {
RUBY_Qfalse = 0,
RUBY_Qtrue = 2,
RUBY_Qnil
= 4,
RUBY_Qundef = 6,
RUBY_IMMEDIATE_MASK = 0x03,
RUBY_FIXNUM_FLAG
= 0x01,
Any VALUE equal to 0 is false,0x0e,
2 is true, 4 is
RUBY_SYMBOL_FLAG
=
nil, and 6 is a special value only 8
RUBY_SPECIAL_SHIFT = used internally.
};
enum ruby_special_consts {
RUBY_Qfalse = 0, on the principle that
Integers and Symbols work
RUBY_Qtrue = allocated without 4-byte
2,
memory is never
RUBY_Qnil
= 4,
alignment.
RUBY_Qundef = 6,
RUBY_IMMEDIATE_MASK
RUBY_FIXNUM_FLAG
RUBY_SYMBOL_FLAG
RUBY_SPECIAL_SHIFT
};

=
=
=
=

0x03,
0x01,
0x0e,
8
enum ruby_special_consts {
Any odd VALUE
RUBY_Qfalse = 0,> 0 is a Fixnum.
RUBY_Qtrue = 2,
An even VALUE not 4,
RUBY_Qnil
= divisible by 4 might be a
RUBY_Qundef =Symbol.
6,
RUBY_IMMEDIATE_MASK
RUBY_FIXNUM_FLAG
RUBY_SYMBOL_FLAG
RUBY_SPECIAL_SHIFT
};

=
=
=
=

0x03,
0x01,
0x0e,
8
Symbols are just integers.
There is a global table mapping
Symbol IDs to the strings they
represent.
Symbols are immediates because
their IDs are stored in VALUE,
and looked up in the symbol
table for display.
CLASS INHERITANCE
We have a pretty good picture of how values are
represented; now we’re going to talk about how
they interact.
class Language
@@random_cvar = true
attr_reader :name
def initialize(name)
@name = name
end
end
basic.klass

Class

ptr->super

Object

iv_tbl

{}

const_tbl

{}

m_tbl

{name: #<M>, initialize: #<M>}

iv_index_tbl

{@@random_cvar: true}
class Ruby < Language
CREATOR = :matz
@origin = :japan
end
basic.klass

Class

ptr->super

Language

iv_tbl

{@origin: :japan}

const_tbl

{CREATOR: :matz}

m_tbl

{} # NB. Empty!

iv_index_tbl

{} # NB. Empty!
When you subclass, you create a
new RClass with
super=(parent) and
klass=Class
When you instantiate a class, you
create a new RObject with
klass=(the class)
Method lookup
Class methods
class Foo
def bar
:baz
end
end

class Foo
def self.bar
:baz
end
end

Foo.new.bar

Foo.baz

We know how
this works now.

But how does
this work?
SINGLETON CLASSES
class Klass
def foo; end
end
obj = Klass.new
def obj.bar ; end

ptr->super
(superclass)

basic.klass
(class)
Image borrowed from Ruby Hacking Guide
Singleton classes get type T_ICLASS.
T_ICLASS objects are never*
returned to ruby-land methods.
*for sufficiently loose definitions of “never”
class
def
end
class
def
end

A
foo ; end
B < A
self.bar ; end

ptr->super
(superclass)

basic.klass
(class)
Image borrowed from Ruby Hacking Guide
class
def
end
class
def
end

A
foo ; end
B < A
self.bar ; end

ptr->super
(superclass)

basic.klass
(class)
Image borrowed from Ruby Hacking Guide
MODULE INHERITANCE
MRI SOURCE SPELUNKING
First, check out the source
github.com/ruby/ruby
google “<your editor> ctags”
CASE STUDY:
How does Array#cycle work?
brb live demo
Builtin types have a <type>.c
(string.c, array.c, proc.c, re.c, etc.)
Interesting methods tend to be in
those files
They are always present inside
double quotes
(easy to search for)
The next parameter after the
string is the C function name
e.g. Search for “upcase” (with the
quotes) in string.c and follow the
chain.
Most of the supporting VM
internals are in vm_*.c
Garbage collection is in gc.c
Don’t look at parse.y.
Trust me.
Almost all of the stuff we’ve
looked at today is in object.c,
class.c, or ruby.h
I mostly look up definitions of
built-in methods
Further reading:
Ruby under a Microscope
http://patshaughnessy.net/ruby-under-a-microscope

Ruby Hacking Guide
http://ruby-hacking-guide.github.io/
Thanks, questions?

More Related Content

What's hot

Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scopesuthi
 
Java best practices
Java best practicesJava best practices
Java best practicesRay Toal
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming PrimerMike Wilcox
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Mirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby GroupMirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby Groupbaroquebobcat
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014hwilming
 
Understanding JavaScript
Understanding JavaScriptUnderstanding JavaScript
Understanding JavaScriptnodejsbcn
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSarah Kiniry
 
Writing Ruby Extensions
Writing Ruby ExtensionsWriting Ruby Extensions
Writing Ruby ExtensionsMatt Todd
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpMichael Girouard
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Mario Camou Riveroll
 
JavaScript 1 for high school
JavaScript 1 for high schoolJavaScript 1 for high school
JavaScript 1 for high schooljekkilekki
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
javascript-cheat-sheet-v1_1
javascript-cheat-sheet-v1_1javascript-cheat-sheet-v1_1
javascript-cheat-sheet-v1_1brecke
 

What's hot (20)

Variables: names, bindings, type, scope
Variables: names, bindings, type, scopeVariables: names, bindings, type, scope
Variables: names, bindings, type, scope
 
Java best practices
Java best practicesJava best practices
Java best practices
 
The JavaScript Programming Primer
The JavaScript  Programming PrimerThe JavaScript  Programming Primer
The JavaScript Programming Primer
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Mirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby GroupMirah Talk for Boulder Ruby Group
Mirah Talk for Boulder Ruby Group
 
00 ruby tutorial
00 ruby tutorial00 ruby tutorial
00 ruby tutorial
 
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
 
Understanding JavaScript
Understanding JavaScriptUnderstanding JavaScript
Understanding JavaScript
 
STC 2016 Programming Language Storytime
STC 2016 Programming Language StorytimeSTC 2016 Programming Language Storytime
STC 2016 Programming Language Storytime
 
Writing Ruby Extensions
Writing Ruby ExtensionsWriting Ruby Extensions
Writing Ruby Extensions
 
Runtime
RuntimeRuntime
Runtime
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?Static or Dynamic Typing? Why not both?
Static or Dynamic Typing? Why not both?
 
JavaScript 1 for high school
JavaScript 1 for high schoolJavaScript 1 for high school
JavaScript 1 for high school
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Parte II Objective C
Parte II   Objective CParte II   Objective C
Parte II Objective C
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
javascript-cheat-sheet-v1_1
javascript-cheat-sheet-v1_1javascript-cheat-sheet-v1_1
javascript-cheat-sheet-v1_1
 

Similar to Learn Ruby by Reading the Source

Metaprogramming in ruby 102: Objects vs. Classes
Metaprogramming in ruby 102: Objects vs. ClassesMetaprogramming in ruby 102: Objects vs. Classes
Metaprogramming in ruby 102: Objects vs. Classesraza_mirza
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactionstepsum
 
NyaruDBにゃるものを使ってみた話 (+Realm比較)
NyaruDBにゃるものを使ってみた話 (+Realm比較)NyaruDBにゃるものを使ってみた話 (+Realm比較)
NyaruDBにゃるものを使ってみた話 (+Realm比較)Masaki Oshikawa
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Workhorse Computing
 
Cassandra Day Chicago 2015: CQL: This is not he SQL you are looking for
Cassandra Day Chicago 2015: CQL: This is not he SQL you are looking forCassandra Day Chicago 2015: CQL: This is not he SQL you are looking for
Cassandra Day Chicago 2015: CQL: This is not he SQL you are looking forDataStax Academy
 
Glorp Tutorial Guide
Glorp Tutorial GuideGlorp Tutorial Guide
Glorp Tutorial GuideESUG
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with examplepranav kumar verma
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in PharoPharo
 
Cowboy way to iOS 8Presentation1
Cowboy way to iOS 8Presentation1Cowboy way to iOS 8Presentation1
Cowboy way to iOS 8Presentation1Carlo Bation
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaDerek Chen-Becker
 

Similar to Learn Ruby by Reading the Source (20)

Metaprogramming in ruby 102: Objects vs. Classes
Metaprogramming in ruby 102: Objects vs. ClassesMetaprogramming in ruby 102: Objects vs. Classes
Metaprogramming in ruby 102: Objects vs. Classes
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactions
 
How to write Ruby extensions with Crystal
How to write Ruby extensions with CrystalHow to write Ruby extensions with Crystal
How to write Ruby extensions with Crystal
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 
Day 2
Day 2Day 2
Day 2
 
Introduction to es6
Introduction to es6Introduction to es6
Introduction to es6
 
Casbase presentation
Casbase presentationCasbase presentation
Casbase presentation
 
Core java concepts
Core java conceptsCore java concepts
Core java concepts
 
Chapter2
Chapter2Chapter2
Chapter2
 
NyaruDBにゃるものを使ってみた話 (+Realm比較)
NyaruDBにゃるものを使ってみた話 (+Realm比較)NyaruDBにゃるものを使ってみた話 (+Realm比較)
NyaruDBにゃるものを使ってみた話 (+Realm比較)
 
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent. Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
Our Friends the Utils: A highway traveled by wheels we didn't re-invent.
 
Cassandra Day Chicago 2015: CQL: This is not he SQL you are looking for
Cassandra Day Chicago 2015: CQL: This is not he SQL you are looking forCassandra Day Chicago 2015: CQL: This is not he SQL you are looking for
Cassandra Day Chicago 2015: CQL: This is not he SQL you are looking for
 
Glorp Tutorial Guide
Glorp Tutorial GuideGlorp Tutorial Guide
Glorp Tutorial Guide
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Cowboy way to iOS 8Presentation1
Cowboy way to iOS 8Presentation1Cowboy way to iOS 8Presentation1
Cowboy way to iOS 8Presentation1
 
A Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to ScalaA Brief, but Dense, Intro to Scala
A Brief, but Dense, Intro to Scala
 
Lecture 3-ARC
Lecture 3-ARCLecture 3-ARC
Lecture 3-ARC
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 

More from Burke Libbey

Nix: What even is it though?
Nix: What even is it though?Nix: What even is it though?
Nix: What even is it though?Burke Libbey
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes BackBurke Libbey
 
Rails Performance Tuning
Rails Performance TuningRails Performance Tuning
Rails Performance TuningBurke Libbey
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes BackBurke Libbey
 
Ruby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other MagicRuby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other MagicBurke Libbey
 

More from Burke Libbey (7)

Nix: What even is it though?
Nix: What even is it though?Nix: What even is it though?
Nix: What even is it though?
 
Coffeescript
CoffeescriptCoffeescript
Coffeescript
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
Fuck Yeah Nouns
Fuck Yeah NounsFuck Yeah Nouns
Fuck Yeah Nouns
 
Rails Performance Tuning
Rails Performance TuningRails Performance Tuning
Rails Performance Tuning
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
Ruby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other MagicRuby's Object Model: Metaprogramming and other Magic
Ruby's Object Model: Metaprogramming and other Magic
 

Recently uploaded

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Learn Ruby by Reading the Source

  • 1. LEARNING RUBY BY READING THE SOURCE tw:@burkelibbey / gh:@burke
  • 2. THESIS: The best way to learn a piece of infrastructure is to learn about how it’s implemented.
  • 3. So let’s dig in to ruby’s source!
  • 4. TOPICS • Basic Object Structure • Class inheritance • Singleton • Module • MRI classes inheritance Source spelunking
  • 6. Every object has an RBasic struct RBasic { VALUE flags; VALUE klass; }
  • 7. flags stores information like whether the object is frozen, tainted, etc. struct RBasic { VALUE flags; VALUE klass; } It’s mostly internal stuff that you don’t think about very often.
  • 8. klass is a pointer to the class of the object struct RBasic { VALUE flags; VALUE klass; } (or singleton class, which we’ll talk about later)
  • 9. ...but what’s a VALUE? struct RBasic { VALUE flags; VALUE klass; }
  • 10. VALUE is basically used as a void pointer. typedef uintptr_t VALUE; It can point to any ruby value.
  • 11. You should interpret “VALUE” as: “a (pointer to a) ruby object”
  • 12. This is a Float. struct RFloat { struct RBasic basic; double float_value; }
  • 13. Every type of object, including Float, has an RBasic. struct RFloat { struct RBasic basic; double float_value; }
  • 14. And then, after the RBasic, type-specific info. struct RFloat { struct RBasic basic; double float_value; }
  • 15. Ruby has quite a few types. Each of them has their own type-specific data fields.
  • 16. But given a ‘VALUE’, we don’t know which type we have. How does ruby know?
  • 17. Every object has an RBasic struct RBasic { VALUE flags; VALUE klass; } And the object type is stored inside flags.
  • 18. Given an object of unknown type... VALUE a struct αѕgє‫נ‬qqωσ { struct RBasic basic; ιηт ‫נ‬ѕƒкq; // ??? ƒ‫נ‬є σтн‫¢נ‬є; // ??? } We can extract the type from ‘basic’, which is guaranteed to be the first struct member.
  • 19. e.g. if the type is T_STRING, struct RString { struct RBasic basic; union { struct { long len; ... then we know it’s a `struct RString`.
  • 20. Every* type corresponds to a struct type, which ALWAYS has an RBasic as the first struct member. * exceptions for immediate values
  • 21. There are custom types for primitives, mostly to make them faster.
  • 22. The special-case primitive types aren’t particularly surprising or interesting.
  • 23. T_STRING => RString RBasic, string data, length. T_ARRAY => RArray RBasic, array data, length. T_HASH => RHash RBasic, hashtable. ...and so on.
  • 24. T_OBJECT (struct RObject) is pretty interesting. It’s what’s used for instances of any classes you define, or most of the standard library.
  • 25. TL;DR: Instance Variables. struct RObject { struct RBasic basic; long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } This makes sense; an instance of a class has its own data, and nothing else.
  • 26. It stores the number of instance variables struct RObject { struct RBasic basic; long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; }
  • 27. struct RObject { struct RBasic basic; long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } And a pointer to a hashtable containing the instance variables
  • 28. This is a shortcut to the class variables of the object’s class. struct RObject { struct RBasic basic; long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } You could get the same result by looking it up on basic.klass (coming up right away)
  • 29. This definition is actually slightly simplified. I omitted another performance optimization for readability. struct RObject { struct RBasic basic; long numiv; VALUE *ivptr; struct st_table *iv_index_tbl; } Go read the full one after this talk if you’re so inclined!
  • 31. Classes have instance variables (ivars), class variables (cvars), methods, and a superclass. struct RClass { struct RBasic basic; rb_classext_t *ptr; struct st_table *m_tbl; struct st_table *iv_index_tbl; }
  • 32. This is where the methods live. struct RClass { struct RBasic basic; rb_classext_t *ptr; struct st_table *m_tbl; struct st_table *iv_index_tbl; } st_table is the hashtable implementation ruby uses internally.
  • 33. Class variables live in iv_index_tbl. struct RClass { struct RBasic basic; rb_classext_t *ptr; struct st_table *m_tbl; struct st_table *iv_index_tbl; }
  • 34. struct RClass { struct RBasic basic; rb_classext_t *ptr; struct st_table *m_tbl; struct st_table *iv_index_tbl; } struct rb_classext_struct { VALUE super; struct st_table *iv_tbl; struct st_table *const_tbl; } typedef struct rb_classext_struct rb_classext_t;
  • 35. The superclass, instance variables, and constants defined inside the class. struct rb_classext_struct { VALUE super; struct st_table *iv_tbl; struct st_table *const_tbl; }
  • 36. It ends up looking kinda like: struct RClass { struct RBasic basic; VALUE super; struct st_table *iv_tbl; struct st_table *const_tbl; struct st_table *m_tbl; struct st_table *iv_index_tbl; } ...though this isn’t really valid because rb_classext_t is referred to by a pointer.
  • 37. So classes have: struct RClass { struct RBasic basic; VALUE super; (st) *iv_tbl; (st) *const_tbl; (st) *m_tbl; (st) *iv_index_tbl; } * * * * * * RBasic superclass instance vars. constants methods class vars.
  • 39. Same underlying type (struct RClass) as a class #define RCLASS(obj) (R_CAST(RClass)(obj)) #define RMODULE(obj) RCLASS(obj) ...just has different handling in a few code paths.
  • 42. For an integer N: The fixnum representation is: 2N + 1
  • 43. enum ruby_special_consts { RUBY_Qfalse = 0, RUBY_Qtrue = 2, RUBY_Qnil = 4, RUBY_Qundef = 6, RUBY_IMMEDIATE_MASK RUBY_FIXNUM_FLAG RUBY_SYMBOL_FLAG RUBY_SPECIAL_SHIFT }; = = = = 0x03, 0x01, 0x0e, 8
  • 44. enum ruby_special_consts { RUBY_Qfalse = 0, RUBY_Qtrue = 2, RUBY_Qnil = 4, RUBY_Qundef = 6, RUBY_IMMEDIATE_MASK = 0x03, RUBY_FIXNUM_FLAG = 0x01, A RUBY_SYMBOL_FLAG a big integer, with a pointer is basically just = 0x0e, number referring to a memory RUBY_SPECIAL_SHIFT = 8 address. };
  • 45. enum ruby_special_consts { RUBY_Qfalse = 0, RUBY_Qtrue = 2, RUBY_Qnil = 4, RUBY_Qundef = 6, Remember how a VALUE is mostly a pointer? RUBY_IMMEDIATE_MASK = 0x03, These tiny addresses are in the0x01, space RUBY_FIXNUM_FLAG = kernel in a process image, which means they’re RUBY_SYMBOL_FLAG = 0x0e, unaddressable. RUBY_SPECIAL_SHIFT = 8 }; So ruby uses them to refer to special values.
  • 46. enum ruby_special_consts { RUBY_Qfalse = 0, RUBY_Qtrue = 2, RUBY_Qnil = 4, RUBY_Qundef = 6, RUBY_IMMEDIATE_MASK = 0x03, RUBY_FIXNUM_FLAG = 0x01, Any VALUE equal to 0 is false,0x0e, 2 is true, 4 is RUBY_SYMBOL_FLAG = nil, and 6 is a special value only 8 RUBY_SPECIAL_SHIFT = used internally. };
  • 47. enum ruby_special_consts { RUBY_Qfalse = 0, on the principle that Integers and Symbols work RUBY_Qtrue = allocated without 4-byte 2, memory is never RUBY_Qnil = 4, alignment. RUBY_Qundef = 6, RUBY_IMMEDIATE_MASK RUBY_FIXNUM_FLAG RUBY_SYMBOL_FLAG RUBY_SPECIAL_SHIFT }; = = = = 0x03, 0x01, 0x0e, 8
  • 48. enum ruby_special_consts { Any odd VALUE RUBY_Qfalse = 0,> 0 is a Fixnum. RUBY_Qtrue = 2, An even VALUE not 4, RUBY_Qnil = divisible by 4 might be a RUBY_Qundef =Symbol. 6, RUBY_IMMEDIATE_MASK RUBY_FIXNUM_FLAG RUBY_SYMBOL_FLAG RUBY_SPECIAL_SHIFT }; = = = = 0x03, 0x01, 0x0e, 8
  • 49. Symbols are just integers.
  • 50. There is a global table mapping Symbol IDs to the strings they represent.
  • 51. Symbols are immediates because their IDs are stored in VALUE, and looked up in the symbol table for display.
  • 53. We have a pretty good picture of how values are represented; now we’re going to talk about how they interact.
  • 54. class Language @@random_cvar = true attr_reader :name def initialize(name) @name = name end end basic.klass Class ptr->super Object iv_tbl {} const_tbl {} m_tbl {name: #<M>, initialize: #<M>} iv_index_tbl {@@random_cvar: true}
  • 55. class Ruby < Language CREATOR = :matz @origin = :japan end basic.klass Class ptr->super Language iv_tbl {@origin: :japan} const_tbl {CREATOR: :matz} m_tbl {} # NB. Empty! iv_index_tbl {} # NB. Empty!
  • 56. When you subclass, you create a new RClass with super=(parent) and klass=Class
  • 57. When you instantiate a class, you create a new RObject with klass=(the class)
  • 58.
  • 59.
  • 60.
  • 62.
  • 63. Class methods class Foo def bar :baz end end class Foo def self.bar :baz end end Foo.new.bar Foo.baz We know how this works now. But how does this work?
  • 65. class Klass def foo; end end obj = Klass.new def obj.bar ; end ptr->super (superclass) basic.klass (class) Image borrowed from Ruby Hacking Guide
  • 66. Singleton classes get type T_ICLASS. T_ICLASS objects are never* returned to ruby-land methods. *for sufficiently loose definitions of “never”
  • 67. class def end class def end A foo ; end B < A self.bar ; end ptr->super (superclass) basic.klass (class) Image borrowed from Ruby Hacking Guide
  • 68. class def end class def end A foo ; end B < A self.bar ; end ptr->super (superclass) basic.klass (class) Image borrowed from Ruby Hacking Guide
  • 70.
  • 71.
  • 72.
  • 74. First, check out the source
  • 76.
  • 77.
  • 79. CASE STUDY: How does Array#cycle work?
  • 81. Builtin types have a <type>.c (string.c, array.c, proc.c, re.c, etc.)
  • 82. Interesting methods tend to be in those files
  • 83. They are always present inside double quotes (easy to search for)
  • 84. The next parameter after the string is the C function name
  • 85. e.g. Search for “upcase” (with the quotes) in string.c and follow the chain.
  • 86. Most of the supporting VM internals are in vm_*.c
  • 88. Don’t look at parse.y. Trust me.
  • 89. Almost all of the stuff we’ve looked at today is in object.c, class.c, or ruby.h
  • 90. I mostly look up definitions of built-in methods
  • 91. Further reading: Ruby under a Microscope http://patshaughnessy.net/ruby-under-a-microscope Ruby Hacking Guide http://ruby-hacking-guide.github.io/