SlideShare ist ein Scribd-Unternehmen logo
1 von 70
RUBY INTERNALS
    Use the source, Luke!




 TW: @burkelibbey GH: @burke
RUBY INTERNALS
    Use the source, Luke!




 TW: @burkelibbey GH: @burke
TOPICS
• Basic   object structure

• Class   inheritance

• Singleton   classes

• Module    inheritance

• Contexts
BASIC OBJECT STRUCTURE
struct RBasic {
                  VALUE flags;
                  VALUE klass;
              };


Every object in ruby has an instance of RBasic.
struct RBasic {
                   VALUE flags;
                   VALUE klass;
               };


flags stores information like whether the object is
           frozen, or tainted, or others.
struct RBasic {
             VALUE flags;
             VALUE klass;
         };


klass is a pointer to the parent class
          (or singleton class)
typedef uintptr_t VALUE;



VALUE is used like a void pointer in ruby C code.
struct RFloat {
    struct RBasic basic;
    double float_value;
};

      This is a float.
struct RFloat {
        struct RBasic basic;
        double float_value;
    };

Like everything else, it has an RBasic.
struct RFloat {
          struct RBasic basic;
          double float_value;
      };

...and also the actual floating point value.
brb c
#define ROBJECT_EMBED_LEN_MAX 3
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;
};

         This is a generic Object.
#define ROBJECT_EMBED_LEN_MAX 3
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;
};

   You can pretty much ignore this stuff.
#define ROBJECT_EMBED_LEN_MAX 3
  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;
  };

Again, it has an RBasic representing its class (klass)
            and internal attributes (flags).
#define ROBJECT_EMBED_LEN_MAX 3
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;
};

       It also has instance variables.
      long numiv;
      VALUE *ivptr;
      struct st_table *iv_index_tbl;




   ivptr points to an array of ivar values.
      long numiv;
      VALUE *ivptr;
      struct st_table *iv_index_tbl;




Unsurprisingly, numiv is the number of ivars.
      long numiv;
      VALUE *ivptr;
      struct st_table *iv_index_tbl;




    iv_index_tbl is essentially a hash of
         {name -> index into ivptr}
      long numiv;
      VALUE *ivptr;
      struct st_table *iv_index_tbl;




 st_table is a C hashtable implementation.
 It’s also the underpinning for ruby hashes.
#define ROBJECT_EMBED_LEN_MAX 3
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;
};

            Back to the top
An Object has:


            • klass   (parent class)

            • flags    (frozen? tainted? etc.)

            • Instance    variables

            • Nothing     else.
brb c
We saw that Float has a distinct implementation
                from Object
String, Regexp, Array, Hash, File, Rational, Complex,
             Data, and Bignum do too.

      These are mostly for performance reasons.
brb c
Class is the other exception.


Not just for performance. It has a lot of extra behaviour.
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;
   };



A class has attributes (flags) and a class (klass).
struct RClass {
      struct RBasic basic;
      rb_classext_t *ptr;
      struct st_table *m_tbl;
      struct st_table *iv_index_tbl;
  };



rb_classext_t stores more class-specific info
struct RClass {
    struct RBasic basic;
    rb_classext_t *ptr;
    struct st_table *m_tbl;
    struct st_table *iv_index_tbl;
};



m_tbl is a hash of methods. Think of it as:
        {name -> method body}
struct RClass {
        struct RBasic basic;
        rb_classext_t *ptr;
        struct st_table *m_tbl;
        struct st_table *iv_index_tbl;
    };



Just like iv_index_tbl on RObject, except the rest
   of the ivar storage is done in rb_classext_t.
struct rb_classext_struct {
    VALUE super;
    struct st_table *iv_tbl;
    struct st_table *const_tbl;
};
typedef struct rb_classext_struct 
   rb_classext_t;



 This is the extended class information.
struct rb_classext_struct {
    VALUE super;
    struct st_table *iv_tbl;
    struct st_table *const_tbl;
};
typedef struct rb_classext_struct 
   rb_classext_t;



‘super’ is a pointer to the class’s superclass.
struct rb_classext_struct {
    VALUE super;
    struct st_table *iv_tbl;
    struct st_table *const_tbl;
};
typedef struct rb_classext_struct 
   rb_classext_t;



iv_tbl is a hash of {ivar name -> ivar value}
struct rb_classext_struct {
    VALUE super;
    struct st_table *iv_tbl;
    struct st_table *const_tbl;
};
typedef struct rb_classext_struct 
   rb_classext_t;



 similarly, const_tbl stores constants as
      {const name -> const value}
struct RClass {
       VALUE flags; //   attributes
       VALUE klass; //   parent class (often Class)
       VALUE super; //   superclass (often Object)
       struct st_table   *iv_tbl;       // ivars
       struct st_table   *const_tbl;    // constants
       struct st_table   *m_tbl;        // methods
       struct st_table   *iv_index_tbl; // ivars
   };




An incorrect but helpful simplification of RClass.
brb c
CLASS INHERITANCE
Let’s look at an example class and its RClass
class Oban < Scotch
  AGE = 14
  @tasty = true
  def tasty
    Oban.instance_variable_get("@tasty")
  end
end
class Oban < Scotch
  AGE = 14
  @tasty = true
  def tasty
    Oban.instance_variable_get("@tasty")
  end
end

 basic.klass               Class
 ptr->super               Scotch
   iv_tbl           {“@tasty” => true}
  const_tbl           {“AGE” => 14}
   m_tbl              {“tasty” => ...}
Another:

   class Animal ; end

class Dog < Animal ; end
Let’s look at another class:
class Scotch < Liquor
  def tasty?
    true
  end
end
class Scotch < Liquor
             def tasty?
               true
             end
           end


This lets us call: Scotch.new.tasty?
class Scotch < Liquor
               def tasty?
                 true
               end
             end


This lets us call: Scotch.new.tasty?
And puts {“tasty?” -> ...} into the m_tbl
class Scotch < Liquor
       def tasty?
         true
       end
     end


What if we wanted ‘tasty?’ to be a class
             method?
It clearly works, but how does ruby know it’s
               a class method?


        class Scotch < Liquor
          def self.tasty?
            true
          end
        end




          There’s only one m_tbl.
SINGLETON CLASSES
When you define a method with
“def self.(name)”, you create a singleton class.
BASIC OBJECT STRUCTURE
BASIC OBJECT STRUCTURE
‘self ’ is not everything it appears to be.
class Foo
               # self == Foo
               def bar
               end
             end

‘bar’ is defined as an instance method of ‘Foo’
class Foo
          def bar
          end
          def self.bar
          end
        end

These should be the same, right?
class Foo
        def bar
        end
        def self.bar
        end
      end

...because these are the same:
      my_method
      self.my_method
class Foo
                def bar
                end
                def self.bar
                end
              end

Invocations use ‘self ’ if no receiver is given.
         Don’t definitions? O_o
NOPE.
def foo Defines on the default definee


def target.bar Defines on target.singleton_class
ie: There’s a second, hidden context.




        default_definee
default_definee
                  Target for method definitions
                  with no target


          self
                  Receiver for method
                  invocations with no receiver
No easy way to reference the default definee.
eval "def _d;end"
y = method(:_d).owner rescue instance_method(:_d).owner
eval "undef _d"
y




             This is ugly, but it works.
DEFINEE = 'eval "def _d;end";y = method(:_d).owner rescue
instance_method(:_d).owner;eval "undef _d";y'

class Foo
  puts eval DEFINEE #=> Foo
end



              Really ugly. Really works.
self != default definee
Changes self?       Changes definee?

     class C                C                     C

  C.class_eval              C                     C

C.instance_eval             C             C.singleton_class

obj.instance_eval          obj            obj.singleton_class

 (in C) def foo            obj            obj.singleton_class

 obj.send :eval            obj             NO CHANGE

  class << obj      obj.singleton_class   obj.singleton_class

Weitere ähnliche Inhalte

Was ist angesagt?

DockerとKubernetesをかけめぐる
DockerとKubernetesをかけめぐるDockerとKubernetesをかけめぐる
DockerとKubernetesをかけめぐるKohei Tokunaga
 
始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~
始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~
始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~貴志 上坂
 
Cours python
Cours pythonCours python
Cours pythonsalmazen
 
レイヤードアーキテクチャを意識したPHPアプリケーションの構築
レイヤードアーキテクチャを意識したPHPアプリケーションの構築レイヤードアーキテクチャを意識したPHPアプリケーションの構築
レイヤードアーキテクチャを意識したPHPアプリケーションの構築Masashi Shinbara
 
ドメイン駆動設計のためのオブジェクト指向入門
ドメイン駆動設計のためのオブジェクト指向入門ドメイン駆動設計のためのオブジェクト指向入門
ドメイン駆動設計のためのオブジェクト指向入門増田 亨
 
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考えるGoのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考えるpospome
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Olve Maudal
 
ADRという考えを取り入れてみて
ADRという考えを取り入れてみてADRという考えを取り入れてみて
ADRという考えを取り入れてみてinfinite_loop
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniShellmates
 
淺談RESTful API認證 Token機制使用經驗分享
淺談RESTful API認證 Token機制使用經驗分享淺談RESTful API認證 Token機制使用經驗分享
淺談RESTful API認證 Token機制使用經驗分享Tun-Yu Chang
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable codeGeshan Manandhar
 
Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références Aziz Darouichi
 
ASP.NETの進化とASP.NET Core Blazorの凄さ
ASP.NETの進化とASP.NET Core Blazorの凄さASP.NETの進化とASP.NET Core Blazorの凄さ
ASP.NETの進化とASP.NET Core Blazorの凄さSho Okada
 
トランザクションスクリプトのすすめ
トランザクションスクリプトのすすめトランザクションスクリプトのすすめ
トランザクションスクリプトのすすめpospome
 
Exercices pascal fenni_2018
Exercices pascal fenni_2018Exercices pascal fenni_2018
Exercices pascal fenni_2018salah fenni
 
REST API のコツ
REST API のコツREST API のコツ
REST API のコツpospome
 
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くかDDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くかKoichiro Matsuoka
 
Notions de base de JavaScript
Notions de base de JavaScriptNotions de base de JavaScript
Notions de base de JavaScriptKristen Le Liboux
 

Was ist angesagt? (20)

Spring と TDD
Spring と TDDSpring と TDD
Spring と TDD
 
DockerとKubernetesをかけめぐる
DockerとKubernetesをかけめぐるDockerとKubernetesをかけめぐる
DockerとKubernetesをかけめぐる
 
始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~
始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~
始めよう! ドメイン駆動設計&マイクロサービス開発 ~C# と Azure Service Fabric で最高の DDD 開発を~
 
Cours python
Cours pythonCours python
Cours python
 
レイヤードアーキテクチャを意識したPHPアプリケーションの構築
レイヤードアーキテクチャを意識したPHPアプリケーションの構築レイヤードアーキテクチャを意識したPHPアプリケーションの構築
レイヤードアーキテクチャを意識したPHPアプリケーションの構築
 
ドメイン駆動設計のためのオブジェクト指向入門
ドメイン駆動設計のためのオブジェクト指向入門ドメイン駆動設計のためのオブジェクト指向入門
ドメイン駆動設計のためのオブジェクト指向入門
 
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考えるGoのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
Goのサーバサイド実装におけるレイヤ設計とレイヤ内実装について考える
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
 
ADRという考えを取り入れてみて
ADRという考えを取り入れてみてADRという考えを取り入れてみて
ADRという考えを取り入れてみて
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
 
淺談RESTful API認證 Token機制使用經驗分享
淺談RESTful API認證 Token機制使用經驗分享淺談RESTful API認證 Token機制使用經驗分享
淺談RESTful API認證 Token機制使用經驗分享
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 
SPARK SQL
SPARK SQLSPARK SQL
SPARK SQL
 
Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références Chapitre4: Pointeurs et références
Chapitre4: Pointeurs et références
 
ASP.NETの進化とASP.NET Core Blazorの凄さ
ASP.NETの進化とASP.NET Core Blazorの凄さASP.NETの進化とASP.NET Core Blazorの凄さ
ASP.NETの進化とASP.NET Core Blazorの凄さ
 
トランザクションスクリプトのすすめ
トランザクションスクリプトのすすめトランザクションスクリプトのすすめ
トランザクションスクリプトのすすめ
 
Exercices pascal fenni_2018
Exercices pascal fenni_2018Exercices pascal fenni_2018
Exercices pascal fenni_2018
 
REST API のコツ
REST API のコツREST API のコツ
REST API のコツ
 
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くかDDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
DDDはオブジェクト指向を利用してどのようにメンテナブルなコードを書くか
 
Notions de base de JavaScript
Notions de base de JavaScriptNotions de base de JavaScript
Notions de base de JavaScript
 

Andere mochten auch

Zend framework dans Azure
Zend framework dans AzureZend framework dans Azure
Zend framework dans Azureafup Paris
 
Ruby without rails
Ruby without railsRuby without rails
Ruby without railsEddie Kao
 
Implementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & CImplementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & CEleanor McHugh
 
Hourglass: a Library for Incremental Processing on Hadoop
Hourglass: a Library for Incremental Processing on HadoopHourglass: a Library for Incremental Processing on Hadoop
Hourglass: a Library for Incremental Processing on HadoopMatthew Hayes
 
Effective IoT System on Openstack
Effective IoT System on OpenstackEffective IoT System on Openstack
Effective IoT System on OpenstackTakashi Kajinami
 
Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...
Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...
Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...Ververica
 
Sparkler - Spark Crawler
Sparkler - Spark Crawler Sparkler - Spark Crawler
Sparkler - Spark Crawler Thamme Gowda
 

Andere mochten auch (7)

Zend framework dans Azure
Zend framework dans AzureZend framework dans Azure
Zend framework dans Azure
 
Ruby without rails
Ruby without railsRuby without rails
Ruby without rails
 
Implementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & CImplementing Virtual Machines in Ruby & C
Implementing Virtual Machines in Ruby & C
 
Hourglass: a Library for Incremental Processing on Hadoop
Hourglass: a Library for Incremental Processing on HadoopHourglass: a Library for Incremental Processing on Hadoop
Hourglass: a Library for Incremental Processing on Hadoop
 
Effective IoT System on Openstack
Effective IoT System on OpenstackEffective IoT System on Openstack
Effective IoT System on Openstack
 
Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...
Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...
Aljoscha Krettek - Apache Flink for IoT: How Event-Time Processing Enables Ea...
 
Sparkler - Spark Crawler
Sparkler - Spark Crawler Sparkler - Spark Crawler
Sparkler - Spark Crawler
 

Ähnlich wie Ruby Internals

Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the SourceBurke Libbey
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in RubyConFoo
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogrammingjoshbuddy
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objectsjulien pauli
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Workhorse Computing
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 
Attributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active recordAttributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active record.toster
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CDavid Wheeler
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactionstepsum
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfranjanadeore1
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013rivierarb
 
Introduction to the Ruby Object Model
Introduction to the Ruby Object ModelIntroduction to the Ruby Object Model
Introduction to the Ruby Object ModelMiki Shiran
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012skinandbones
 
Journey of a C# developer into Javascript
Journey of a C# developer into JavascriptJourney of a C# developer into Javascript
Journey of a C# developer into JavascriptMassimo Franciosa
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby MetaprogrammingNando Vieira
 

Ähnlich wie Ruby Internals (20)

Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the Source
 
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
 
About Python
About PythonAbout Python
About Python
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.Object Trampoline: Why having not the object you want is what you need.
Object Trampoline: Why having not the object you want is what you need.
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Attributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active recordAttributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active record
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Building and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning CBuilding and Distributing PostgreSQL Extensions Without Learning C
Building and Distributing PostgreSQL Extensions Without Learning C
 
Javascript
JavascriptJavascript
Javascript
 
Oracle Objects And Transactions
Oracle Objects And TransactionsOracle Objects And Transactions
Oracle Objects And Transactions
 
JavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdfJavaScript Cheatsheets with easy way .pdf
JavaScript Cheatsheets with easy way .pdf
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Introduction to the Ruby Object Model
Introduction to the Ruby Object ModelIntroduction to the Ruby Object Model
Introduction to the Ruby Object Model
 
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
 
Journey of a C# developer into Javascript
Journey of a C# developer into JavascriptJourney of a C# developer into Javascript
Journey of a C# developer into Javascript
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

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
 
🐬 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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
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
 
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
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
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
 

Kürzlich hochgeladen (20)

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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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...
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
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
 
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
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 

Ruby Internals

  • 1. RUBY INTERNALS Use the source, Luke! TW: @burkelibbey GH: @burke
  • 2. RUBY INTERNALS Use the source, Luke! TW: @burkelibbey GH: @burke
  • 3. TOPICS • Basic object structure • Class inheritance • Singleton classes • Module inheritance • Contexts
  • 5. struct RBasic {     VALUE flags;     VALUE klass; }; Every object in ruby has an instance of RBasic.
  • 6. struct RBasic {     VALUE flags;     VALUE klass; }; flags stores information like whether the object is frozen, or tainted, or others.
  • 7. struct RBasic {     VALUE flags;     VALUE klass; }; klass is a pointer to the parent class (or singleton class)
  • 8. typedef uintptr_t VALUE; VALUE is used like a void pointer in ruby C code.
  • 9. struct RFloat {     struct RBasic basic;     double float_value; }; This is a float.
  • 10. struct RFloat {     struct RBasic basic;     double float_value; }; Like everything else, it has an RBasic.
  • 11. struct RFloat {     struct RBasic basic;     double float_value; }; ...and also the actual floating point value.
  • 12. brb c
  • 13. #define ROBJECT_EMBED_LEN_MAX 3 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; }; This is a generic Object.
  • 14. #define ROBJECT_EMBED_LEN_MAX 3 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; }; You can pretty much ignore this stuff.
  • 15. #define ROBJECT_EMBED_LEN_MAX 3 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; }; Again, it has an RBasic representing its class (klass) and internal attributes (flags).
  • 16. #define ROBJECT_EMBED_LEN_MAX 3 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; }; It also has instance variables.
  • 17.       long numiv;       VALUE *ivptr;       struct st_table *iv_index_tbl; ivptr points to an array of ivar values.
  • 18.       long numiv;       VALUE *ivptr;       struct st_table *iv_index_tbl; Unsurprisingly, numiv is the number of ivars.
  • 19.       long numiv;       VALUE *ivptr;       struct st_table *iv_index_tbl; iv_index_tbl is essentially a hash of {name -> index into ivptr}
  • 20.       long numiv;       VALUE *ivptr;       struct st_table *iv_index_tbl; st_table is a C hashtable implementation. It’s also the underpinning for ruby hashes.
  • 21. #define ROBJECT_EMBED_LEN_MAX 3 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; }; Back to the top
  • 22. An Object has: • klass (parent class) • flags (frozen? tainted? etc.) • Instance variables • Nothing else.
  • 23. brb c
  • 24. We saw that Float has a distinct implementation from Object
  • 25. String, Regexp, Array, Hash, File, Rational, Complex, Data, and Bignum do too. These are mostly for performance reasons.
  • 26. brb c
  • 27. Class is the other exception. Not just for performance. It has a lot of extra behaviour.
  • 28. struct RClass {     struct RBasic basic;     rb_classext_t *ptr;     struct st_table *m_tbl;     struct st_table *iv_index_tbl; };
  • 29. struct RClass {     struct RBasic basic;     rb_classext_t *ptr;     struct st_table *m_tbl;     struct st_table *iv_index_tbl; }; A class has attributes (flags) and a class (klass).
  • 30. struct RClass {     struct RBasic basic;     rb_classext_t *ptr;     struct st_table *m_tbl;     struct st_table *iv_index_tbl; }; rb_classext_t stores more class-specific info
  • 31. struct RClass {     struct RBasic basic;     rb_classext_t *ptr;     struct st_table *m_tbl;     struct st_table *iv_index_tbl; }; m_tbl is a hash of methods. Think of it as: {name -> method body}
  • 32. struct RClass {     struct RBasic basic;     rb_classext_t *ptr;     struct st_table *m_tbl;     struct st_table *iv_index_tbl; }; Just like iv_index_tbl on RObject, except the rest of the ivar storage is done in rb_classext_t.
  • 33. struct rb_classext_struct {     VALUE super;     struct st_table *iv_tbl;     struct st_table *const_tbl; }; typedef struct rb_classext_struct rb_classext_t; This is the extended class information.
  • 34. struct rb_classext_struct {     VALUE super;     struct st_table *iv_tbl;     struct st_table *const_tbl; }; typedef struct rb_classext_struct rb_classext_t; ‘super’ is a pointer to the class’s superclass.
  • 35. struct rb_classext_struct {     VALUE super;     struct st_table *iv_tbl;     struct st_table *const_tbl; }; typedef struct rb_classext_struct rb_classext_t; iv_tbl is a hash of {ivar name -> ivar value}
  • 36. struct rb_classext_struct {     VALUE super;     struct st_table *iv_tbl;     struct st_table *const_tbl; }; typedef struct rb_classext_struct rb_classext_t; similarly, const_tbl stores constants as {const name -> const value}
  • 37. struct RClass {     VALUE flags; // attributes     VALUE klass; // parent class (often Class)     VALUE super; // superclass (often Object)     struct st_table *iv_tbl; // ivars     struct st_table *const_tbl; // constants     struct st_table *m_tbl; // methods     struct st_table *iv_index_tbl; // ivars }; An incorrect but helpful simplification of RClass.
  • 38. brb c
  • 40. Let’s look at an example class and its RClass
  • 41. class Oban < Scotch   AGE = 14   @tasty = true   def tasty     Oban.instance_variable_get("@tasty")   end end
  • 42. class Oban < Scotch   AGE = 14   @tasty = true   def tasty     Oban.instance_variable_get("@tasty")   end end basic.klass Class ptr->super Scotch iv_tbl {“@tasty” => true} const_tbl {“AGE” => 14} m_tbl {“tasty” => ...}
  • 43. Another: class Animal ; end class Dog < Animal ; end
  • 44.
  • 45.
  • 46. Let’s look at another class:
  • 47. class Scotch < Liquor   def tasty?     true   end end
  • 48. class Scotch < Liquor   def tasty?     true   end end This lets us call: Scotch.new.tasty?
  • 49. class Scotch < Liquor   def tasty?     true   end end This lets us call: Scotch.new.tasty? And puts {“tasty?” -> ...} into the m_tbl
  • 50. class Scotch < Liquor   def tasty?     true   end end What if we wanted ‘tasty?’ to be a class method?
  • 51. It clearly works, but how does ruby know it’s a class method? class Scotch < Liquor   def self.tasty?     true   end end There’s only one m_tbl.
  • 53. When you define a method with “def self.(name)”, you create a singleton class.
  • 56.
  • 57. ‘self ’ is not everything it appears to be.
  • 58. class Foo   # self == Foo   def bar   end end ‘bar’ is defined as an instance method of ‘Foo’
  • 59. class Foo   def bar   end   def self.bar   end end These should be the same, right?
  • 60. class Foo   def bar   end   def self.bar   end end ...because these are the same: my_method self.my_method
  • 61. class Foo   def bar   end   def self.bar   end end Invocations use ‘self ’ if no receiver is given. Don’t definitions? O_o
  • 62. NOPE.
  • 63. def foo Defines on the default definee def target.bar Defines on target.singleton_class
  • 64. ie: There’s a second, hidden context. default_definee
  • 65. default_definee Target for method definitions with no target self Receiver for method invocations with no receiver
  • 66. No easy way to reference the default definee.
  • 67. eval "def _d;end" y = method(:_d).owner rescue instance_method(:_d).owner eval "undef _d" y This is ugly, but it works.
  • 68. DEFINEE = 'eval "def _d;end";y = method(:_d).owner rescue instance_method(:_d).owner;eval "undef _d";y' class Foo   puts eval DEFINEE #=> Foo end Really ugly. Really works.
  • 69. self != default definee
  • 70. Changes self? Changes definee? class C C C C.class_eval C C C.instance_eval C C.singleton_class obj.instance_eval obj obj.singleton_class (in C) def foo obj obj.singleton_class obj.send :eval obj NO CHANGE class << obj obj.singleton_class obj.singleton_class

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n