attr_bool 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rdoc_options +27 -0
- data/Gemfile +12 -1
- data/LICENSE.txt +1 -1
- data/README.md +305 -0
- data/Rakefile +90 -86
- data/attr_bool.gemspec +35 -31
- data/lib/attr_bool/core_ext.rb +3 -13
- data/lib/attr_bool/version.rb +2 -3
- data/lib/attr_bool.rb +179 -132
- data/test/attr_bool_test.rb +17 -0
- data/test/core_ext_test.rb +103 -0
- data/test/ext_test.rb +432 -0
- data/test/ref_test.rb +142 -0
- data/test/test_helper.rb +18 -0
- metadata +38 -100
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 427185e002ab3a1eea72403d4b91428dca1d981049fc9b79abd2b445add682b7
|
4
|
+
data.tar.gz: 71328c73417dcdff6c82600d46bc15347c9aa5ca65ea73f4310d4d6b55518a9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bfaabe00faf88460c364ff76a978eb8adbfc2cd40d481bdb05cab9d8eb565c2634a796042af6bc08d5e3b4b16744140c4d9f8dfce3da14780cf8e0ed43eb857
|
7
|
+
data.tar.gz: eb3fadb07b363bba497d12b05d112c762d1b44c66c99c6bd5b0decb0374044f2cd92055116c18f2508111c6f0ffa243a526bf6c64695992c36563e26aa3bf462
|
data/.rdoc_options
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
---
|
2
|
+
encoding: UTF-8
|
3
|
+
op_dir: doc
|
4
|
+
title: AttrBool
|
5
|
+
main_page: README.md
|
6
|
+
|
7
|
+
apply_default_exclude: true
|
8
|
+
embed_mixins: true
|
9
|
+
force_update: true
|
10
|
+
hyperlink_all: true
|
11
|
+
line_numbers: true
|
12
|
+
markup: markdown
|
13
|
+
show_hash: true
|
14
|
+
skip_tests: true
|
15
|
+
|
16
|
+
exclude:
|
17
|
+
- /.git/
|
18
|
+
- /.github/
|
19
|
+
- /.idea/
|
20
|
+
- /doc/
|
21
|
+
- /pkg/
|
22
|
+
- /spec/
|
23
|
+
- /stock/
|
24
|
+
- /test/
|
25
|
+
- Gemfile
|
26
|
+
- Gemfile.lock
|
27
|
+
- Rakefile
|
data/Gemfile
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
4
|
source 'https://rubygems.org'
|
6
5
|
|
7
6
|
gemspec
|
7
|
+
|
8
|
+
group(:development,:test) do
|
9
|
+
gem 'benchmark','~> 0.4 ' # For benchmarks in Rakefile.
|
10
|
+
gem 'bundler' ,'~> 2.6 '
|
11
|
+
gem 'rake' ,'~> 13.3 '
|
12
|
+
gem 'rdoc' ,'~> 6.14' # Doc.
|
13
|
+
gem 'simplecov','~> 0.22' # Test coverage.
|
14
|
+
end
|
15
|
+
|
16
|
+
group(:test) do
|
17
|
+
gem 'minitest','~> 5.25'
|
18
|
+
end
|
data/LICENSE.txt
CHANGED
data/README.md
ADDED
@@ -0,0 +1,305 @@
|
|
1
|
+
# AttrBool
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/attr_bool)
|
4
|
+
[](https://github.com/esotericpig/attr_bool/actions/workflows/ruby.yml)
|
5
|
+
[](https://github.com/esotericpig/attr_bool)
|
6
|
+
[](CHANGELOG.md)
|
7
|
+
[](LICENSE.txt)
|
8
|
+
|
9
|
+
Easily create `attr` (attribute) methods that end with question marks (`?`) for booleans/predicates.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'attr_bool'
|
13
|
+
|
14
|
+
class TheTodd
|
15
|
+
extend AttrBool::Ext
|
16
|
+
#using AttrBool::Ref # Can use refinements instead.
|
17
|
+
|
18
|
+
attr_accessor? :headband
|
19
|
+
attr_reader? :banana_hammock
|
20
|
+
attr_writer? :high_five
|
21
|
+
|
22
|
+
# Can do DSL chaining.
|
23
|
+
protected attr_accessor? :carla_kiss, :elliot_kiss
|
24
|
+
|
25
|
+
# Can force bool values (i.e., only `true` or `false`).
|
26
|
+
attr_bool :bounce_pecs # Accessor.
|
27
|
+
attr_bool? :cat_fight # Reader.
|
28
|
+
attr_bool! :hot_tub # Writer.
|
29
|
+
end
|
30
|
+
|
31
|
+
todd = TheTodd.new
|
32
|
+
|
33
|
+
puts todd.headband?
|
34
|
+
puts todd.banana_hammock?
|
35
|
+
puts todd.bounce_pecs?
|
36
|
+
puts todd.cat_fight?
|
37
|
+
```
|
38
|
+
|
39
|
+
Features:
|
40
|
+
- Can use multiple symbols and/or strings.
|
41
|
+
- Can force bool values.
|
42
|
+
- Can define custom logic with a block/proc.
|
43
|
+
- Returns an array of the new method names, just like the core `attr` methods, to allow DSL chaining.
|
44
|
+
- Can use refinements (`using AttrBool::Ref`) instead of `extend`.
|
45
|
+
- This allows you to refine the top module only of your project, instead of having to extend every class.
|
46
|
+
- Fails fast if an instance variable name is invalid (if you don't use a block/proc).
|
47
|
+
|
48
|
+
Anti-features:
|
49
|
+
- No default values.
|
50
|
+
- Initialize your instance variables in `def initialize` like normal.
|
51
|
+
- Using default values has performance issues and other drawbacks, so better to just match the core `attr` methods.
|
52
|
+
- Uses inner `AttrBool::Ext` & `AttrBool::Ref` instead of `AttrBool`.
|
53
|
+
- Some gems use the `extend AttrBool` (top module) pattern, but this includes `VERSION` in all of your classes/modules.
|
54
|
+
- Doesn't monkey-patch the core class/module by default.
|
55
|
+
- If desired for apps/scripts, you still can with `require 'attr_bool/core_ext'`, but not recommended for libraries.
|
56
|
+
|
57
|
+
## // Contents
|
58
|
+
|
59
|
+
- [Similar Projects](#-similar-projects)
|
60
|
+
- [Setup](#-setup)
|
61
|
+
- [Usage](#-usage)
|
62
|
+
- [RuboCop](#-rubocop)
|
63
|
+
- [YARDoc](#-yardoc)
|
64
|
+
- [Hacking](#-hacking)
|
65
|
+
- [Benchmarks](#-benchmarks)
|
66
|
+
- [License](#-license)
|
67
|
+
|
68
|
+
## [//](#-contents) Similar Projects
|
69
|
+
|
70
|
+
Create a [discussion](https://github.com/esotericpig/attr_bool/discussions) or an [issue](https://github.com/esotericpig/attr_bool/issues) to let me know to add your project.
|
71
|
+
|
72
|
+
| Gem Name | Code | Example |
|
73
|
+
|--------------------------------------------------------------------------|---------------------------------------------------------------|-----------------------------------------------------------|
|
74
|
+
| [attr_asker](https://rubygems.org/gems/attr_asker) | [GitHub](https://github.com/kitlangton/attr_asker) | `attr_asker :winning` |
|
75
|
+
| [attr_boolean](https://rubygems.org/gems/attr_boolean) | [GitHub](https://github.com/talentnest/attr_boolean) | `attr_boolean :winning, default: true` |
|
76
|
+
| [attr_setting](https://rubygems.org/gems/attr_setting) | [GitHub](https://github.com/merhard/attr_setting) | `attr_setting :winning, true` |
|
77
|
+
| [attribool](https://rubygems.org/gems/attribool) | [GitHub](https://github.com/evanthegrayt/attribool) | `bool_reader :winning` |
|
78
|
+
| [attribute_boolean](https://rubygems.org/gems/attribute_boolean) | [GitHub](https://github.com/alexmchale/attribute_boolean) | `attr_boolean :winning` |
|
79
|
+
| [attribute_predicates](https://rubygems.org/gems/attribute_predicates) | [GitHub](https://github.com/pluginaweek/attribute_predicates) | `attr :winning, true` |
|
80
|
+
| [boolean_accessor](https://rubygems.org/gems/boolean_accessor) | [GitHub](https://github.com/hiroki23/boolean_accessor) | `battr_accessor :winning` |
|
81
|
+
| [named_accessors](https://rubygems.org/gems/named_accessors) | [GitHub](https://github.com/zlw/named_accessors) | `named_reader :winning, as: :winning?` |
|
82
|
+
| [predicateable](https://rubygems.org/gems/predicateable) | [GitHub](https://github.com/nsgc/predicateable) | `predicate :wins, [:losing, :winning]` |
|
83
|
+
| [predicates](https://rubygems.org/gems/predicates) | [GitHub](https://github.com/Erol/predicates) | `predicate :winning?` |
|
84
|
+
| [property-accessor](https://rubygems.org/gems/property-accessor) | [GitHub](https://github.com/estepnv/property-accessor) | `property(:winning) { get(:winning?); default { true } }` |
|
85
|
+
| [question_mark_methods](https://rubygems.org/gems/question_mark_methods) | [GitHub](https://github.com/poiyzy/questionmarkmethods) | `add_question_mark_methods winning?: :winning` |
|
86
|
+
| [wannabe_bool](https://rubygems.org/gems/wannabe_bool) | [GitHub](https://github.com/prodis/wannabe_bool) | `attr_wannabe_bool :winning` |
|
87
|
+
| [wardrobe](https://rubygems.org/gems/wardrobe) | [GitHub](https://github.com/agensdev/wardrobe) | `attribute :winning, Wardrobe::Boolean, default: true` |
|
88
|
+
|
89
|
+
Searches:
|
90
|
+
|
91
|
+
| Site | Searches |
|
92
|
+
|------------------|--------------------------------------------------------------------------------------------------------------------------|
|
93
|
+
| The Ruby Toolbox | [1](https://www.ruby-toolbox.com/search?q=attr+bool), [2](https://www.ruby-toolbox.com/search?q=predicate) |
|
94
|
+
| RubyGems.org | [1](https://rubygems.org/search?query=attr+OR+attribute), [2](https://rubygems.org/search?query=predicates+OR+predicate) |
|
95
|
+
|
96
|
+
## [//](#-contents) Setup
|
97
|
+
|
98
|
+
Pick your poison...
|
99
|
+
|
100
|
+
With the *RubyGems* package manager:
|
101
|
+
|
102
|
+
```bash
|
103
|
+
gem install attr_bool
|
104
|
+
```
|
105
|
+
|
106
|
+
Or in your *Gemspec*:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
spec.add_dependency 'attr_bool', '~> X.X'
|
110
|
+
```
|
111
|
+
|
112
|
+
Or in your *Gemfile*:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
# Pick your poison...
|
116
|
+
gem 'attr_bool', '~> X.X'
|
117
|
+
gem 'attr_bool', git: 'https://github.com/esotericpig/attr_bool.git'
|
118
|
+
```
|
119
|
+
|
120
|
+
Or from source:
|
121
|
+
|
122
|
+
```bash
|
123
|
+
git clone --depth 1 'https://github.com/esotericpig/attr_bool.git'
|
124
|
+
cd attr_bool
|
125
|
+
bundle install
|
126
|
+
bundle exec rake install:local
|
127
|
+
```
|
128
|
+
|
129
|
+
## [//](#-contents) Usage
|
130
|
+
|
131
|
+
You can either add `extend AttrBool::Ext` in your class/module, add `using AttrBool::Ref` in your class/module, or include `require 'attr_bool/core_ext'`.
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
require 'attr_bool'
|
135
|
+
|
136
|
+
class TheTodd
|
137
|
+
extend AttrBool::Ext
|
138
|
+
#using AttrBool::Ref # Can use refinements instead.
|
139
|
+
|
140
|
+
# Can use multiple symbols and/or strings.
|
141
|
+
attr_accessor? :flexing, 'bounce_pecs'
|
142
|
+
|
143
|
+
# Can do DSL chaining.
|
144
|
+
protected attr_accessor? :high_five, 'fist_bump'
|
145
|
+
|
146
|
+
# Can do custom logic.
|
147
|
+
attr_accessor? :headband, 'banana_hammock',
|
148
|
+
reader: -> { @wearing == :flaming },
|
149
|
+
writer: ->(value) { @wearing = value }
|
150
|
+
|
151
|
+
attr_reader?(:cat_fights) { @cat_fights % 69 }
|
152
|
+
attr_writer?(:hot_surgeries) { |count| @hot_surgeries += count }
|
153
|
+
|
154
|
+
# Can force bool values (i.e., only `true` or `false`).
|
155
|
+
attr_bool :carla_kiss # Accessor.
|
156
|
+
attr_bool? :elliot_kiss # Reader.
|
157
|
+
attr_bool! :thumbs_up # Writer.
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
If you don't want to have to add `extend AttrBool::Ext` to every class/module, you can simply refine the top module in your gem/library/app:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
require 'attr_bool'
|
165
|
+
|
166
|
+
module TheToddMod
|
167
|
+
using AttrBool::Ref
|
168
|
+
end
|
169
|
+
|
170
|
+
# --- Some other file.
|
171
|
+
module TheToddMod
|
172
|
+
class TheTodd
|
173
|
+
attr_bool :banana_hammock
|
174
|
+
end
|
175
|
+
end
|
176
|
+
```
|
177
|
+
|
178
|
+
If you only have an app/script (**not** a library), then you can simply include `require 'attr_bool/core_ext'` to monkey-patch the core class & module:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
require 'attr_bool/core_ext'
|
182
|
+
|
183
|
+
class TheTodd
|
184
|
+
attr_bool :banana_hammock
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
### [///](#-contents) RuboCop
|
189
|
+
|
190
|
+
RuboCop might complain about `Layout/EmptyLinesAroundAttributeAccessor`:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
class TheTodd
|
194
|
+
attr_accessor? :banana_hammock
|
195
|
+
attr_accessor :headband
|
196
|
+
attr_accessor? :bounce_pecs
|
197
|
+
end
|
198
|
+
```
|
199
|
+
|
200
|
+
You can either disable this Cop or adjust it accordingly:
|
201
|
+
|
202
|
+
```yaml
|
203
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
204
|
+
#Enabled: false
|
205
|
+
AllowedMethods:
|
206
|
+
- attr_accessor?
|
207
|
+
- attr_reader?
|
208
|
+
- attr_writer?
|
209
|
+
- attr_bool
|
210
|
+
- attr_bool?
|
211
|
+
- attr_bool!
|
212
|
+
```
|
213
|
+
|
214
|
+
### [///](#-contents) YARDoc
|
215
|
+
|
216
|
+
Here are some examples of how to document the methods in YARDoc:
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
attr_accessor? :winning # @!attribute [rw] winning=(value),winning?
|
220
|
+
attr_reader? :running # @!attribute [r] running?
|
221
|
+
|
222
|
+
# @!attribute [r] can_swim?
|
223
|
+
# @return [true,false] can you swim in it?
|
224
|
+
# @!attribute [r] can_wink?
|
225
|
+
# @return [true,false] can you wink at pretty people?
|
226
|
+
attr_reader? :can_swim,:can_wink
|
227
|
+
|
228
|
+
# @!attribute [rw] princess=(value),princess?
|
229
|
+
# @param value [true,false] this is Ms. Consuela or not!
|
230
|
+
# @return [true,false] is this Ms. Consuela?
|
231
|
+
# @!attribute [rw] crap_bag=(value),crap_bag?
|
232
|
+
# @param value [true,false] this is Mr. Crap Bag or not!
|
233
|
+
# @return [true,false] is this Mr. Crap Bag?
|
234
|
+
attr_accessor? :princess,:crap_bag
|
235
|
+
|
236
|
+
# @overload in_fashion?
|
237
|
+
# @return [true,false] whether it's fashionable right now
|
238
|
+
# @overload in_fashion=(value)
|
239
|
+
# Make it in or out of fashion!
|
240
|
+
attr_accessor? :in_fashion
|
241
|
+
|
242
|
+
# @!group My Attrs
|
243
|
+
# @!attribute [r] in_season?
|
244
|
+
attr_reader? :in_season
|
245
|
+
# @!attribute [r] can_wash?
|
246
|
+
attr_reader? :can_wash
|
247
|
+
# @!endgroup
|
248
|
+
```
|
249
|
+
|
250
|
+
Further reading:
|
251
|
+
|
252
|
+
- [Documenting Attributes](https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md#documenting-attributes)
|
253
|
+
- [Documenting Custom DSL Methods](https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md#documenting-custom-dsl-methods)
|
254
|
+
- [Tags#Attribute](https://www.rubydoc.info/gems/yard/file/docs/Tags.md#attribute)
|
255
|
+
- [Macros](https://www.rubydoc.info/gems/yard/file/docs/GettingStarted.md#macros)
|
256
|
+
- [Tags#Macro](https://www.rubydoc.info/gems/yard/file/docs/Tags.md#macro)
|
257
|
+
- [Writing Handlers](https://yardoc.org/guides/extending-yard/writing-handlers.html)
|
258
|
+
- [YARD::Handlers::Ruby::AttributeHandler](https://github.com/lsegal/yard/blob/main/lib/yard/handlers/ruby/attribute_handler.rb)
|
259
|
+
|
260
|
+
## [//](#-contents) Hacking
|
261
|
+
|
262
|
+
```bash
|
263
|
+
git clone 'https://github.com/esotericpig/attr_bool.git'
|
264
|
+
cd attr_bool
|
265
|
+
bundle install
|
266
|
+
bundle exec rake -T
|
267
|
+
```
|
268
|
+
|
269
|
+
Run tests:
|
270
|
+
|
271
|
+
```bash
|
272
|
+
bundle exec rake test
|
273
|
+
```
|
274
|
+
|
275
|
+
Generate doc:
|
276
|
+
|
277
|
+
```bash
|
278
|
+
bundle exec rake doc
|
279
|
+
```
|
280
|
+
|
281
|
+
Install locally:
|
282
|
+
|
283
|
+
```bash
|
284
|
+
bundle exec rake install:local
|
285
|
+
```
|
286
|
+
|
287
|
+
### [///](#-contents) Benchmarks
|
288
|
+
|
289
|
+
Benchmarks are kind of meaningless, but after playing around with some, I found the following to be true on my system:
|
290
|
+
- `define_method()` is faster than `class/module_eval()`.
|
291
|
+
- `? true : false` (ternary operator) is faster than `!!` (surprisingly).
|
292
|
+
|
293
|
+
Therefore, AttrBool uses the "faster" ones found.
|
294
|
+
|
295
|
+
To run these on your system:
|
296
|
+
|
297
|
+
```bash
|
298
|
+
bundle exec rake bench
|
299
|
+
```
|
300
|
+
|
301
|
+
## [//](#-contents) License
|
302
|
+
|
303
|
+
AttrBool (https://github.com/esotericpig/attr_bool)
|
304
|
+
Copyright (c) 2020-2025 Bradley Whited
|
305
|
+
[MIT License](LICENSE.txt)
|
data/Rakefile
CHANGED
@@ -1,140 +1,144 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
4
|
require 'bundler/gem_tasks'
|
6
5
|
|
6
|
+
require 'attr_bool/version'
|
7
7
|
require 'benchmark'
|
8
8
|
require 'rake/clean'
|
9
9
|
require 'rake/testtask'
|
10
|
-
require '
|
11
|
-
|
12
|
-
require 'attr_bool/version'
|
10
|
+
require 'rdoc/task'
|
13
11
|
|
14
|
-
CLEAN.exclude('{.git,stock}/**/*')
|
12
|
+
CLEAN.exclude('{.git,.github,.idea,stock}/**/*')
|
15
13
|
CLOBBER.include('doc/')
|
16
14
|
|
17
|
-
task default: [
|
18
|
-
|
19
|
-
desc 'Generate doc'
|
20
|
-
task :doc,%i[] => %i[yard] do |task|
|
21
|
-
# pass
|
22
|
-
end
|
15
|
+
task default: %i[test]
|
23
16
|
|
24
|
-
desc '
|
25
|
-
task :
|
26
|
-
ENV['doctest'] = 'y'
|
17
|
+
desc 'Run all tests'
|
18
|
+
task test: %i[test:base test:core_ext]
|
27
19
|
|
28
|
-
|
20
|
+
TEST_DIR = 'test/**'
|
21
|
+
CORE_EXT_TEST = 'core_ext_test.rb'
|
29
22
|
|
30
|
-
|
31
|
-
|
23
|
+
namespace :test do
|
24
|
+
{
|
25
|
+
base: FileList["#{TEST_DIR}/*_test.rb"].exclude("**/#{CORE_EXT_TEST}"),
|
26
|
+
core_ext: FileList["#{TEST_DIR}/#{CORE_EXT_TEST}"],
|
27
|
+
}.each do |name,test_files|
|
28
|
+
Rake::TestTask.new(name) do |t|
|
29
|
+
t.libs = ['lib','test']
|
30
|
+
t.test_files = test_files
|
31
|
+
t.warning = true
|
32
|
+
t.verbose = false
|
33
|
+
end
|
34
|
+
end
|
32
35
|
end
|
33
36
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
task.pattern = File.join('test','**','*_test.rb')
|
38
|
-
task.description += ": '#{task.pattern}'"
|
39
|
-
task.verbose = false
|
40
|
-
task.warning = true
|
37
|
+
RDoc::Task.new(:doc) do |t|
|
38
|
+
t.rdoc_dir = 'doc'
|
39
|
+
t.title = "AttrBool v#{AttrBool::VERSION}"
|
41
40
|
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
42
|
+
# Benchmarks are kind of meaningless, but after playing around with some,
|
43
|
+
# I found the following to be true on my system:
|
44
|
+
# - define_method() is faster than class/module_eval().
|
45
|
+
# - `? true : false` (ternary operator) is faster than `!!` (surprisingly).
|
46
|
+
desc 'Benchmark code related to AttrBool'
|
47
|
+
task :bench do
|
48
|
+
bench_def_methods
|
49
|
+
bench_force_bools
|
50
|
+
puts
|
52
51
|
end
|
53
52
|
|
54
|
-
|
55
|
-
|
56
|
-
# rubocop:disable all
|
53
|
+
def bench_def_methods
|
54
|
+
# rubocop:disable Style/DocumentDynamicEvalDefinition,Style/EvalWithLocation
|
57
55
|
|
58
|
-
|
59
|
-
N1 = 20_000_000
|
56
|
+
n = 200_000
|
60
57
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
58
|
+
puts
|
59
|
+
Benchmark.bmbm do |bm|
|
60
|
+
bm.report('class_eval ') do
|
61
|
+
Class.new do
|
62
|
+
n.times do |i|
|
63
|
+
name = "bool_#{i}"
|
64
|
+
class_eval("def #{name}?; @#{name}; end")
|
65
|
+
end
|
67
66
|
end
|
68
67
|
end
|
69
68
|
|
70
|
-
|
71
|
-
|
72
|
-
n
|
73
|
-
|
74
|
-
|
75
|
-
instance_variable_get(:"@#{n}")
|
69
|
+
bm.report('define_method') do
|
70
|
+
Class.new do
|
71
|
+
n.times do |i|
|
72
|
+
name = "bool_#{i}"
|
73
|
+
define_method(:"#{name}?") { instance_variable_get(:"@#{name}") }
|
76
74
|
end
|
77
75
|
end
|
78
76
|
end
|
79
77
|
|
80
|
-
|
81
|
-
|
82
|
-
n
|
83
|
-
|
84
|
-
|
78
|
+
bm.report('module_eval ') do
|
79
|
+
Class.new do
|
80
|
+
n.times do |i|
|
81
|
+
name = "bool_#{i}"
|
82
|
+
module_eval("def #{name}?; @#{name}; end")
|
83
|
+
end
|
85
84
|
end
|
86
85
|
end
|
87
86
|
end
|
88
87
|
|
89
|
-
|
88
|
+
# rubocop:enable all
|
89
|
+
end
|
90
|
+
|
91
|
+
def bench_force_bools
|
92
|
+
# rubocop:disable Style/DoubleNegation
|
93
|
+
|
94
|
+
n = 5_000_000
|
95
|
+
values = ['str',1,0,1.0,0.0,nil,true,false]
|
90
96
|
|
91
97
|
puts
|
92
98
|
Benchmark.bmbm do |bm|
|
93
|
-
bm.report('
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
class DefineMethodTest
|
101
|
-
do_define_method :dm
|
99
|
+
bm.report('?:') do
|
100
|
+
n.times do
|
101
|
+
x = nil
|
102
|
+
values.each do |value|
|
103
|
+
x = value ? true : false
|
104
|
+
end
|
105
|
+
x
|
102
106
|
end
|
103
107
|
end
|
104
108
|
|
105
|
-
bm.report('
|
106
|
-
|
107
|
-
|
109
|
+
bm.report('!!') do
|
110
|
+
n.times do
|
111
|
+
x = nil
|
112
|
+
values.each do |value|
|
113
|
+
x = !!value
|
114
|
+
end
|
115
|
+
x
|
108
116
|
end
|
109
117
|
end
|
110
118
|
end
|
111
119
|
|
112
|
-
str = 'str' # Warning workaround
|
113
|
-
|
114
120
|
puts
|
115
121
|
Benchmark.bmbm do |bm|
|
116
|
-
bm.report('
|
117
|
-
|
118
|
-
x =
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
x
|
122
|
+
bm.report('!!') do
|
123
|
+
n.times do
|
124
|
+
x = nil
|
125
|
+
values.each do |value|
|
126
|
+
x = !!value
|
127
|
+
end
|
128
|
+
x
|
123
129
|
end
|
124
130
|
end
|
125
131
|
|
126
|
-
bm.report('
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
132
|
+
bm.report('?:') do
|
133
|
+
n.times do
|
134
|
+
x = nil
|
135
|
+
values.each do |value|
|
136
|
+
x = value ? true : false
|
137
|
+
end
|
138
|
+
x
|
133
139
|
end
|
134
140
|
end
|
135
141
|
end
|
136
142
|
|
137
|
-
puts
|
138
|
-
|
139
143
|
# rubocop:enable all
|
140
144
|
end
|