SlideShare a Scribd company logo
1 of 45
よしだあつし
低レイヤ(メモリ)からみた
ruby プログラミング
自己紹介
• 名前: 吉田篤(よしだあつし)
• 肩書: 株式会社おけや代表取締役CTO
• 職業: ゲームプログラマ(cocos2d-x, rails)
• Twitter他: yalab
• ruby歴: 12年ぐらい?
• rails歴: 10年ぐらい?
PR: まじょのおしごと
グッズ通販
shop.oke-ya.com
PR: てんしのおしごと
クラウドファンディング実施中
https://www.makuake.com/project/oke-ya/
目次
• プログラムって何してるの?
• メモリと型
• メモリ上の class と object
• メソッド呼び出し
注意点
• rubyの実装は調べましたがこの資料は正確さに
はやや欠けます
Q. プログラムって
何してるの?
A. メモリ上の数値を
CPUで書き換えている
メモリと型
(2進数)
0110_0001
• 数値だと思うと97
• 文字列だと思うと”a”
• (処理系によって解釈は変わります)
ruby処理系でCPU でメモリ上の
数値を書き換えてみる
• (2進数) 0110_0001 #=> “a”
• “a”.gsub!(“a”, “b”)
• (2進数) 0110_0010 #=> “b”
• # 実際の ruby の動きとは違います
メモリ上の
class と object
Q. class や object
って何?
A. class はたい焼きの型で
object はたい焼き
はぁ?
Q. class や object
って何?
A.class は RClass 構造体。
object は RClass の object_id を持つ
RBasic を内部的に持つ
RObject 構造体
はぁ?
構造体?
構造体?
struct Human {
char name[255];
int age;
};
Human user;
メモ
リ
??? name age ???
Human
RClass
struct RClass {
struct RBasic basic;
VALUE super;
rb_classext_t *ptr;
struct rb_id_table *m_tbl;
};
メモ
リ
basic super ptr m_tbl
https://github.com/ruby/ruby/blob/ruby_2_3/internal.h#L485
RObjectstruct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
void *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
メモ
リ
basic super ptr m_tbl
https://github.com/ruby/ruby/blob/ruby_2_3/include/ruby/ruby.h#L907
RBasic
struct RBasic {
VALUE flags;
const VALUE klass;
}
メモ
リ
flags value
https://github.com/ruby/ruby/blob/ruby_2_3/include/ruby/ruby.h#L872
A.class は RClass 構造体。
object は RClass の object_id を持つ
RBasic を内部的に持つ
RObject 構造体
ruby は RClass や RObject
を使ってるらしい
ruby のメモリ利用
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
man = Person.new("taro", 4)
woman = Person.new("hanako", 31)
メモ
リ
空き
→
実
行
前
ruby のメモリ利用
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
man = Person.new("taro", 4)
woman = Person.new("hanako", 31)
メモ
リ
RClass(Person) 空き
こ
こ
ま
で
解
釈
→
ruby のメモリ利用
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
man = Person.new("taro", 4)
woman = Person.new("hanako", 31)
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
こ
こ
ま
で
解
釈
→
ruby のメモリ利用
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
man = Person.new("taro", 4)
woman = Person.new("hanako", 31)
こ
こ
ま
で
解
釈
→
メモ
リ
RClass(Person) RObject(man) "taro" 4 RObject(woman) "hanako" 31 空き
ruby のメモリ利用
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
man = Person.new("taro", 4)
woman = Person.new("hanako", 31)
こ
こ
ま
で
解
釈
→
object_id
メモ
リ
RClass(Person) RObject(man) "taro" 4 RObject(woman) "hanako" 31 空き
ruby は RClass や RObject
をメモリ上に作る
- class 定義時
- new した時
- リテラルを書いた時
メソッド呼び出し
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
void *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
struct RClass {
struct RBasic basic;
VALUE super;
rb_classext_t *ptr;
struct rb_id_table *m_tbl;
};
メソッド呼び出し
class Person
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
man = Person.new("taro", 4)
man.name #=> “taro”
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
メソッド呼び出し
man.name #=> “taro”
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
void *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
};
メソッド呼び出し
man.name #=> “taro”
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
klass
struct RBasic {
VALUE flags;
const VALUE klass;
}
メソッド呼び出し
man.name #=> “taro”
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
struct RBasic {
VALUE flags;
const VALUE klass;
}
klass
メソッド呼び出し
man.name #=> “taro”
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
struct RClass {
struct RBasic basic;
VALUE super;
rb_classext_t *ptr;
struct rb_id_table *m_tbl;
};
m_tbl
メソッド呼び出し
man.name #=> “taro”
メモ
リ
RClass(Person) RObject(man) "taro" 4 空き
m_tbl
struct RObject {
struct RBasic basic;
union {
struct {
long numiv;
VALUE *ivptr;
void *iv_index_tbl;
} heap;
VALUE ary[ROBJECT_EMBED_LEN_MAX];
} as;
ここからは僕の主張
たい焼き VS 構造体
プログラミング
抽象化され過ぎ
一つ下のレイヤを見る
と理解が進みます
今回の資料作りのために
cruby 読んで
大変勉強になりました
ご清聴
ありがとうございました

More Related Content

Viewers also liked

デバッガでデバッグしない
デバッガでデバッグしないデバッガでデバッグしない
デバッガでデバッグしないよしだ あつし
 
Twitterでネットストーカーをしよう
TwitterでネットストーカーをしようTwitterでネットストーカーをしよう
Twitterでネットストーカーをしようよしだ あつし
 
私はいかにしてpull request を行ったか - あるいは social development について
私はいかにしてpull request を行ったか - あるいは social development について私はいかにしてpull request を行ったか - あるいは social development について
私はいかにしてpull request を行ったか - あるいは social development についてよしだ あつし
 
よい名前を付けましょう リーダブルなんたらとか
よい名前を付けましょう   リーダブルなんたらとかよい名前を付けましょう   リーダブルなんたらとか
よい名前を付けましょう リーダブルなんたらとかよしだ あつし
 
まじょのおしごとの裏側
まじょのおしごとの裏側まじょのおしごとの裏側
まじょのおしごとの裏側よしだ あつし
 
15分でできるSQLインジェクション
15分でできるSQLインジェクション15分でできるSQLインジェクション
15分でできるSQLインジェクションよしだ あつし
 
GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ
 GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ  GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ
GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ Hiroyuki Tanaka
 
Cocos2d-xハンズオン#1 in 大阪
Cocos2d-xハンズオン#1 in 大阪Cocos2d-xハンズオン#1 in 大阪
Cocos2d-xハンズオン#1 in 大阪Shingo Yamano
 

Viewers also liked (15)

Rails3使用雑感
Rails3使用雑感Rails3使用雑感
Rails3使用雑感
 
Railsの今昔
Railsの今昔Railsの今昔
Railsの今昔
 
デバッガでデバッグしない
デバッガでデバッグしないデバッガでデバッグしない
デバッガでデバッグしない
 
Twitterでネットストーカーをしよう
TwitterでネットストーカーをしようTwitterでネットストーカーをしよう
Twitterでネットストーカーをしよう
 
私はいかにしてpull request を行ったか - あるいは social development について
私はいかにしてpull request を行ったか - あるいは social development について私はいかにしてpull request を行ったか - あるいは social development について
私はいかにしてpull request を行ったか - あるいは social development について
 
よい名前を付けましょう リーダブルなんたらとか
よい名前を付けましょう   リーダブルなんたらとかよい名前を付けましょう   リーダブルなんたらとか
よい名前を付けましょう リーダブルなんたらとか
 
テスト駆動開発入門
テスト駆動開発入門テスト駆動開発入門
テスト駆動開発入門
 
まじょのおしごとの裏側
まじょのおしごとの裏側まじょのおしごとの裏側
まじょのおしごとの裏側
 
15分でできるSQLインジェクション
15分でできるSQLインジェクション15分でできるSQLインジェクション
15分でできるSQLインジェクション
 
Dockerプレゼン
DockerプレゼンDockerプレゼン
Dockerプレゼン
 
Gitの使い方あれこれ
Gitの使い方あれこれGitの使い方あれこれ
Gitの使い方あれこれ
 
Vue.js入門
Vue.js入門Vue.js入門
Vue.js入門
 
GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ
 GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ  GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ
GCS2013 リーンソフトウェア開発から見るゲーム開発7つのムダ
 
SICPの紹介
SICPの紹介SICPの紹介
SICPの紹介
 
Cocos2d-xハンズオン#1 in 大阪
Cocos2d-xハンズオン#1 in 大阪Cocos2d-xハンズオン#1 in 大阪
Cocos2d-xハンズオン#1 in 大阪
 

Similar to 低レイヤから見たrubyプログラミング

~knitr+pandocではじめる~『R MarkdownでReproducible Research』
~knitr+pandocではじめる~『R MarkdownでReproducible Research』~knitr+pandocではじめる~『R MarkdownでReproducible Research』
~knitr+pandocではじめる~『R MarkdownでReproducible Research』Nagi Teramo
 
10分で分かるRパッケージの作り方
10分で分かるRパッケージの作り方10分で分かるRパッケージの作り方
10分で分かるRパッケージの作り方Yohei Sato
 
セマンテックウェブとRDFDB
セマンテックウェブとRDFDBセマンテックウェブとRDFDB
セマンテックウェブとRDFDBHirosuke Asano
 
Integral - New O/R Mapper for Common Lisp
Integral - New O/R Mapper for Common LispIntegral - New O/R Mapper for Common Lisp
Integral - New O/R Mapper for Common Lispfukamachi
 
Ruby Sapporo Night Vol4
Ruby Sapporo Night Vol4Ruby Sapporo Night Vol4
Ruby Sapporo Night Vol4Koji SHIMADA
 
名古屋Ruby会議02 LT:Ruby中級への道
名古屋Ruby会議02 LT:Ruby中級への道名古屋Ruby会議02 LT:Ruby中級への道
名古屋Ruby会議02 LT:Ruby中級への道Shigeru UCHIYAMA
 
Scalaで萌える関数型プログラミング[完全版]
Scalaで萌える関数型プログラミング[完全版]Scalaで萌える関数型プログラミング[完全版]
Scalaで萌える関数型プログラミング[完全版]Ra Zon
 
Code Reading at Security and Programming camp 2011
Code Reading at Security and Programming camp 2011 Code Reading at Security and Programming camp 2011
Code Reading at Security and Programming camp 2011 Hiro Yoshioka
 
つながるデータShare
つながるデータShareつながるデータShare
つながるデータShareSeiji Koide
 
Programming camp code reading
Programming camp code readingProgramming camp code reading
Programming camp code readingHiro Yoshioka
 
ARC-009_RDB 技術者のための NoSQL ガイド
ARC-009_RDB 技術者のための NoSQL ガイドARC-009_RDB 技術者のための NoSQL ガイド
ARC-009_RDB 技術者のための NoSQL ガイドdecode2016
 
SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回
SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回
SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回yamahige
 
Pythonista による Pythonista のための Scala 紹介 in BPStudy #49
Pythonista による Pythonista のための Scala 紹介 in BPStudy #49Pythonista による Pythonista のための Scala 紹介 in BPStudy #49
Pythonista による Pythonista のための Scala 紹介 in BPStudy #49shoma h
 
Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]Ra Zon
 
Alluren of prototype-based OOP
Alluren of prototype-based OOPAlluren of prototype-based OOP
Alluren of prototype-based OOPazuma satoshi
 
Yamadai.R チュートリアルセッション
Yamadai.R チュートリアルセッションYamadai.R チュートリアルセッション
Yamadai.R チュートリアルセッション考司 小杉
 
HiroshimaR6_Introduction
HiroshimaR6_IntroductionHiroshimaR6_Introduction
HiroshimaR6_IntroductionSAKAUE, Tatsuya
 
activerecord-oracle_enhanced-adapterのご紹介
activerecord-oracle_enhanced-adapterのご紹介activerecord-oracle_enhanced-adapterのご紹介
activerecord-oracle_enhanced-adapterのご紹介Kevin Toyoda
 

Similar to 低レイヤから見たrubyプログラミング (20)

~knitr+pandocではじめる~『R MarkdownでReproducible Research』
~knitr+pandocではじめる~『R MarkdownでReproducible Research』~knitr+pandocではじめる~『R MarkdownでReproducible Research』
~knitr+pandocではじめる~『R MarkdownでReproducible Research』
 
10分で分かるRパッケージの作り方
10分で分かるRパッケージの作り方10分で分かるRパッケージの作り方
10分で分かるRパッケージの作り方
 
セマンテックウェブとRDFDB
セマンテックウェブとRDFDBセマンテックウェブとRDFDB
セマンテックウェブとRDFDB
 
Integral - New O/R Mapper for Common Lisp
Integral - New O/R Mapper for Common LispIntegral - New O/R Mapper for Common Lisp
Integral - New O/R Mapper for Common Lisp
 
Ruby Sapporo Night Vol4
Ruby Sapporo Night Vol4Ruby Sapporo Night Vol4
Ruby Sapporo Night Vol4
 
名古屋Ruby会議02 LT:Ruby中級への道
名古屋Ruby会議02 LT:Ruby中級への道名古屋Ruby会議02 LT:Ruby中級への道
名古屋Ruby会議02 LT:Ruby中級への道
 
Scalaで萌える関数型プログラミング[完全版]
Scalaで萌える関数型プログラミング[完全版]Scalaで萌える関数型プログラミング[完全版]
Scalaで萌える関数型プログラミング[完全版]
 
Code Reading at Security and Programming camp 2011
Code Reading at Security and Programming camp 2011 Code Reading at Security and Programming camp 2011
Code Reading at Security and Programming camp 2011
 
つながるデータShare
つながるデータShareつながるデータShare
つながるデータShare
 
Programming camp code reading
Programming camp code readingProgramming camp code reading
Programming camp code reading
 
ARC-009_RDB 技術者のための NoSQL ガイド
ARC-009_RDB 技術者のための NoSQL ガイドARC-009_RDB 技術者のための NoSQL ガイド
ARC-009_RDB 技術者のための NoSQL ガイド
 
Tokyo r38
Tokyo r38Tokyo r38
Tokyo r38
 
SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回
SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回
SPARQLから入門するLinked Open Data(LOD)ハンズオン 第2回
 
Pythonista による Pythonista のための Scala 紹介 in BPStudy #49
Pythonista による Pythonista のための Scala 紹介 in BPStudy #49Pythonista による Pythonista のための Scala 紹介 in BPStudy #49
Pythonista による Pythonista のための Scala 紹介 in BPStudy #49
 
Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]Scalaで萌える関数型プログラミング[1.1.RC1]
Scalaで萌える関数型プログラミング[1.1.RC1]
 
Alluren of prototype-based OOP
Alluren of prototype-based OOPAlluren of prototype-based OOP
Alluren of prototype-based OOP
 
Yamadai.R チュートリアルセッション
Yamadai.R チュートリアルセッションYamadai.R チュートリアルセッション
Yamadai.R チュートリアルセッション
 
HiroshimaR6_Introduction
HiroshimaR6_IntroductionHiroshimaR6_Introduction
HiroshimaR6_Introduction
 
activerecord-oracle_enhanced-adapterのご紹介
activerecord-oracle_enhanced-adapterのご紹介activerecord-oracle_enhanced-adapterのご紹介
activerecord-oracle_enhanced-adapterのご紹介
 
Scrum alliance regional gathering tokyo 2013 pub
Scrum alliance regional gathering tokyo 2013 pubScrum alliance regional gathering tokyo 2013 pub
Scrum alliance regional gathering tokyo 2013 pub
 

低レイヤから見たrubyプログラミング