rabbit-slide-znz-frozen_string_literal 2017.01.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rabbit +1 -0
- data/README.md +23 -0
- data/Rakefile +17 -0
- data/config.yaml +20 -0
- data/frozen_string_literal.md +295 -0
- data/pdf/frozen_string_literal-frozen_string_literal.pdf +0 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e31e579cd00677369848b11bed282e05ed1069af
|
4
|
+
data.tar.gz: 4b0885aaaef7ef7e6b9454097640f31276ae2859
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ff234bf29cf8b3c308b09230a2831cd038ebc70a656041fd0160845040fc4f568757462d18a90792772a4c3698aef202cf3f405162bd1d2288e0808ee25119d
|
7
|
+
data.tar.gz: d10bcf8609ddfa79df67d42589212198a1cabc28bb1f46edae05992d418759a93eacfbee601de6a5c304c9d1fbc528358f5b5c5667ae7a48ac7b61ec6993398b
|
data/.rabbit
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
frozen_string_literal.md
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# `frozen_string_literal` の話
|
2
|
+
|
3
|
+
ruby 2.3.0 で追加された `frozen_string_literal` についてちょっと詳しく話します。
|
4
|
+
|
5
|
+
## 作者向け
|
6
|
+
|
7
|
+
### 表示
|
8
|
+
|
9
|
+
rake
|
10
|
+
|
11
|
+
### 公開
|
12
|
+
|
13
|
+
rake publish
|
14
|
+
|
15
|
+
## 閲覧者向け
|
16
|
+
|
17
|
+
### インストール
|
18
|
+
|
19
|
+
gem install rabbit-slide-znz-frozen_string_literal
|
20
|
+
|
21
|
+
### 表示
|
22
|
+
|
23
|
+
rabbit rabbit-slide-znz-frozen_string_literal.gem
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "rabbit/task/slide"
|
2
|
+
|
3
|
+
# Edit ./config.yaml to customize meta data
|
4
|
+
|
5
|
+
spec = nil
|
6
|
+
Rabbit::Task::Slide.new do |task|
|
7
|
+
spec = task.spec
|
8
|
+
# spec.files += Dir.glob("doc/**/*.*")
|
9
|
+
# spec.files -= Dir.glob("private/**/*.*")
|
10
|
+
# spec.add_runtime_dependency("YOUR THEME")
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Tag #{spec.version}"
|
14
|
+
task :tag do
|
15
|
+
sh("git", "tag", "-a", spec.version.to_s, "-m", "Publish #{spec.version}")
|
16
|
+
sh("git", "push", "--tags")
|
17
|
+
end
|
data/config.yaml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
---
|
2
|
+
id: frozen_string_literal
|
3
|
+
base_name: frozen_string_literal
|
4
|
+
tags:
|
5
|
+
- ruby
|
6
|
+
presentation_date: '2017-01-14'
|
7
|
+
version: 2017.01.14
|
8
|
+
licenses: []
|
9
|
+
slideshare_id: frozenstringliteral
|
10
|
+
speaker_deck_id: frozen-string-literal-falsehua
|
11
|
+
ustream_id:
|
12
|
+
vimeo_id:
|
13
|
+
youtube_id:
|
14
|
+
author:
|
15
|
+
markup_language: :markdown
|
16
|
+
name: Kazuhiro NISHIYAMA
|
17
|
+
email: zn@mbf.nifty.com
|
18
|
+
rubygems_user: znz
|
19
|
+
slideshare_user: znzjp
|
20
|
+
speaker_deck_user: znz
|
@@ -0,0 +1,295 @@
|
|
1
|
+
# `frozen_string_literal` の話
|
2
|
+
|
3
|
+
author
|
4
|
+
: Kazuhiro NISHIYAMA
|
5
|
+
|
6
|
+
content-source
|
7
|
+
: 第76回 Ruby関西 勉強会
|
8
|
+
|
9
|
+
date
|
10
|
+
: 2017-01-14
|
11
|
+
|
12
|
+
allotted-time
|
13
|
+
: 15m
|
14
|
+
|
15
|
+
# 自己紹介
|
16
|
+
|
17
|
+
- 西山和広
|
18
|
+
- id:znz (github, twitter など)
|
19
|
+
- Ruby コミッター
|
20
|
+
|
21
|
+
# `frozen_string_literal` とは?
|
22
|
+
|
23
|
+
- ruby 2.3.0 以降
|
24
|
+
- 文字列リテラルで生成される文字列を freeze されたものにする機能
|
25
|
+
- たとえば `''.frozen?` が `true` になる
|
26
|
+
|
27
|
+
# 使い方
|
28
|
+
|
29
|
+
- ファイルの頭にマジックコメント (ファイルごとに指定)
|
30
|
+
|
31
|
+
```
|
32
|
+
# frozen_string_literal: true
|
33
|
+
```
|
34
|
+
|
35
|
+
- 起動時のオプション (プロセス全体のデフォルトを指定)
|
36
|
+
|
37
|
+
```
|
38
|
+
--enable=frozen_string_literal
|
39
|
+
```
|
40
|
+
|
41
|
+
# 優先度
|
42
|
+
|
43
|
+
- 両方指定された場合はマジックコメント優先
|
44
|
+
|
45
|
+
# オプションの例
|
46
|
+
|
47
|
+
```
|
48
|
+
% ruby --enable=frozen_string_literal \
|
49
|
+
-e 'p "".frozen?'
|
50
|
+
true
|
51
|
+
% ruby --disable=frozen_string_literal \
|
52
|
+
-e 'p "".frozen?'
|
53
|
+
false
|
54
|
+
```
|
55
|
+
|
56
|
+
- 対応していない gem が存在する可能性があるので enable にするときは動作確認必須
|
57
|
+
|
58
|
+
# マジックコメントの例
|
59
|
+
|
60
|
+
shebang, coding もつけるときはこの順番
|
61
|
+
|
62
|
+
```
|
63
|
+
% cat a.rb
|
64
|
+
#!/usr/bin/ruby
|
65
|
+
# coding: ascii-8bit
|
66
|
+
# frozen_string_literal: true
|
67
|
+
p ''.frozen?
|
68
|
+
p ''.encoding
|
69
|
+
% ruby a.rb
|
70
|
+
true
|
71
|
+
#<Encoding:ASCII-8BIT>
|
72
|
+
```
|
73
|
+
|
74
|
+
# 利点
|
75
|
+
|
76
|
+
オブジェクト生成が減る
|
77
|
+
|
78
|
+
```
|
79
|
+
% ruby -e "2.times { p ''.object_id }"
|
80
|
+
16501180
|
81
|
+
16501040
|
82
|
+
% cat a.rb
|
83
|
+
# frozen_string_literal: true
|
84
|
+
2.times { p ''.object_id }
|
85
|
+
% ruby a.rb
|
86
|
+
11393540
|
87
|
+
11393540
|
88
|
+
```
|
89
|
+
|
90
|
+
# `"".freeze`
|
91
|
+
|
92
|
+
ruby 2.1.0 から `"文字列リテラル".freeze` は最適化が入っている
|
93
|
+
|
94
|
+
```
|
95
|
+
% ruby -e "2.times { p ''.freeze.object_id }"
|
96
|
+
16865720
|
97
|
+
16865720
|
98
|
+
```
|
99
|
+
|
100
|
+
# 欠点
|
101
|
+
|
102
|
+
破壊的変更が必要な場合に対処が必要
|
103
|
+
|
104
|
+
```
|
105
|
+
% cat a.rb
|
106
|
+
# frozen_string_literal: true
|
107
|
+
s = ''
|
108
|
+
s << 'x'
|
109
|
+
p s
|
110
|
+
% ruby a.rb
|
111
|
+
a.rb:3:in `<main>': can't modify frozen String (RuntimeError)
|
112
|
+
```
|
113
|
+
|
114
|
+
# 破壊的変更が必要な場合
|
115
|
+
|
116
|
+
`String#dup` を使う
|
117
|
+
|
118
|
+
```
|
119
|
+
% cat a.rb
|
120
|
+
# frozen_string_literal: true
|
121
|
+
s = ''.dup
|
122
|
+
s << 'x'
|
123
|
+
p s
|
124
|
+
% ruby a.rb
|
125
|
+
"x"
|
126
|
+
```
|
127
|
+
|
128
|
+
# 別の方法
|
129
|
+
|
130
|
+
`String.new` を使う方法もある
|
131
|
+
|
132
|
+
```
|
133
|
+
% cat a.rb
|
134
|
+
# frozen_string_literal: true
|
135
|
+
s = String.new
|
136
|
+
s << 'x'
|
137
|
+
p s
|
138
|
+
% ruby a.rb
|
139
|
+
"x"
|
140
|
+
```
|
141
|
+
|
142
|
+
# 違い
|
143
|
+
|
144
|
+
`Encoding` が違う
|
145
|
+
|
146
|
+
```
|
147
|
+
% cat a.rb
|
148
|
+
# frozen_string_literal: true
|
149
|
+
p ''.dup.frozen?
|
150
|
+
p ''.dup.encoding
|
151
|
+
p String.new.frozen?
|
152
|
+
p String.new.encoding
|
153
|
+
% ruby a.rb
|
154
|
+
false
|
155
|
+
#<Encoding:UTF-8>
|
156
|
+
false
|
157
|
+
#<Encoding:ASCII-8BIT>
|
158
|
+
```
|
159
|
+
|
160
|
+
# frozen になるリテラル例 (1)
|
161
|
+
|
162
|
+
`''` や `""` の文字列リテラルは frozen になる
|
163
|
+
|
164
|
+
```
|
165
|
+
% cat a.rb
|
166
|
+
# frozen_string_literal: true
|
167
|
+
p 'string'.frozen?
|
168
|
+
p "string\n".frozen?
|
169
|
+
% ruby a.rb
|
170
|
+
true
|
171
|
+
true
|
172
|
+
```
|
173
|
+
|
174
|
+
# frozen になるリテラル例 (2)
|
175
|
+
|
176
|
+
文字列補間 (string interpolation) があっても文字列全体は frozen になる
|
177
|
+
|
178
|
+
```
|
179
|
+
% cat a.rb
|
180
|
+
# frozen_string_literal: true
|
181
|
+
p "#{1+2}".frozen?
|
182
|
+
% ruby a.rb
|
183
|
+
true
|
184
|
+
```
|
185
|
+
|
186
|
+
# frozen になるリテラル例 (3)
|
187
|
+
|
188
|
+
文字リテラルも frozen
|
189
|
+
|
190
|
+
```
|
191
|
+
% cat a.rb
|
192
|
+
# frozen_string_literal: true
|
193
|
+
p ?a.frozen?
|
194
|
+
% ruby a.rb
|
195
|
+
true
|
196
|
+
```
|
197
|
+
|
198
|
+
# frozen になるリテラル例 (4)
|
199
|
+
|
200
|
+
`%` 記法でもヒアドキュメントでも frozen になる
|
201
|
+
|
202
|
+
```
|
203
|
+
% cat a.rb
|
204
|
+
# frozen_string_literal: true
|
205
|
+
p [%q(q), %Q[#{1+2}], <<END].all?(&:frozen?)
|
206
|
+
#{1+2}
|
207
|
+
END
|
208
|
+
% ruby a.rb
|
209
|
+
true
|
210
|
+
```
|
211
|
+
|
212
|
+
# frozen になるリテラル例 (5)
|
213
|
+
|
214
|
+
`%w` は配列自体は frozen ではないが、文字列は frozen
|
215
|
+
|
216
|
+
```
|
217
|
+
% cat a.rb
|
218
|
+
# frozen_string_literal: true
|
219
|
+
a = %w[a b]
|
220
|
+
p a.frozen?
|
221
|
+
p a.all?(&:frozen?)
|
222
|
+
% ruby a.rb
|
223
|
+
false
|
224
|
+
true
|
225
|
+
```
|
226
|
+
|
227
|
+
# frozen になるリテラル例 (6)
|
228
|
+
|
229
|
+
`%W` も同様
|
230
|
+
|
231
|
+
```
|
232
|
+
% cat a.rb
|
233
|
+
# frozen_string_literal: true
|
234
|
+
v = "c d"
|
235
|
+
a = %W(a\ b #{v}e\sf #{})
|
236
|
+
p a.frozen?
|
237
|
+
p a.all?(&:frozen?)
|
238
|
+
% ruby a.rb
|
239
|
+
false
|
240
|
+
true
|
241
|
+
```
|
242
|
+
|
243
|
+
# frozen ではないリテラル例
|
244
|
+
|
245
|
+
外部コマンド出力は frozen ではない
|
246
|
+
|
247
|
+
```
|
248
|
+
% cat a.rb
|
249
|
+
# frozen_string_literal: true
|
250
|
+
p `echo`.frozen?, %x(echo).frozen?
|
251
|
+
% ruby a.rb
|
252
|
+
false
|
253
|
+
false
|
254
|
+
```
|
255
|
+
|
256
|
+
# disasm で確認 (1)
|
257
|
+
|
258
|
+
- `putstring` は `String` を作成
|
259
|
+
- `freeze` があると `opt_str_freeze` になる
|
260
|
+
|
261
|
+
```
|
262
|
+
% ruby --dump=insns -e '""'
|
263
|
+
== disasm: #<ISeq:<main>@-e>============================================
|
264
|
+
0000 trace 1 ( 1)
|
265
|
+
0002 putstring ""
|
266
|
+
0004 leave
|
267
|
+
% ruby --dump=insns -e '"".freeze'
|
268
|
+
== disasm: #<ISeq:<main>@-e>============================================
|
269
|
+
0000 trace 1 ( 1)
|
270
|
+
0002 opt_str_freeze ""
|
271
|
+
0004 leave
|
272
|
+
```
|
273
|
+
|
274
|
+
# disasm で確認 (2)
|
275
|
+
|
276
|
+
- `freeze` は再定義チェックが入る
|
277
|
+
- `putobject` はオブジェクトをスタックに積むだけ
|
278
|
+
|
279
|
+
```
|
280
|
+
% ruby --enable=frozen_string_literal --dump=insns -e '"".freeze'
|
281
|
+
== disasm: #<ISeq:<main>@-e>============================================
|
282
|
+
0000 trace 1 ( 1)
|
283
|
+
0002 opt_str_freeze ""
|
284
|
+
0004 leave
|
285
|
+
% ruby --enable=frozen_string_literal --dump=insns -e '""'
|
286
|
+
== disasm: #<ISeq:<main>@-e>============================================
|
287
|
+
0000 trace 1 ( 1)
|
288
|
+
0002 putobject ""
|
289
|
+
0004 leave
|
290
|
+
```
|
291
|
+
|
292
|
+
# まとめ
|
293
|
+
|
294
|
+
- ruby 2.3.0 以降を使うなら `frozen_string_literal: true` を積極的につけましょう
|
295
|
+
- `freeze` は不要になるので ruby 2.3.0 以降だけを対象にするのなら消すと良い
|
Binary file
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rabbit-slide-znz-frozen_string_literal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2017.01.14
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kazuhiro NISHIYAMA
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rabbit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.2
|
27
|
+
description: ruby 2.3.0 で追加された `frozen_string_literal` についてちょっと詳しく話します。
|
28
|
+
email:
|
29
|
+
- zn@mbf.nifty.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rabbit"
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- config.yaml
|
38
|
+
- frozen_string_literal.md
|
39
|
+
- pdf/frozen_string_literal-frozen_string_literal.pdf
|
40
|
+
homepage: http://slide.rabbit-shocker.org/authors/znz/frozen_string_literal/
|
41
|
+
licenses: []
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.5.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: "`frozen_string_literal` の話"
|
63
|
+
test_files: []
|