attr_bool 0.2.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 157027292cdc404395192f6372dca22a286c93a25a8be537e762f45f22c72ffe
4
- data.tar.gz: 9922d7205c2b06d5a0a63fb5fff54f6c8901a43c082435645be514ff3a5cee16
3
+ metadata.gz: 427185e002ab3a1eea72403d4b91428dca1d981049fc9b79abd2b445add682b7
4
+ data.tar.gz: 71328c73417dcdff6c82600d46bc15347c9aa5ca65ea73f4310d4d6b55518a9b
5
5
  SHA512:
6
- metadata.gz: 2a729e5654e55f3952556251ec67490dd9bb20ddd45b0564336fad0d24c38d1a8f862f03d04bb075debbd02851a33de35fbc9e02d9770a78f2bc35b9a7ee6d12
7
- data.tar.gz: 1dab140de4d8f01fb50870117a775dc522c63426118c6f78cd71076da2769b1874217586f79e7288542f2f71b63f8f23b3857b01fc5fa5eb8e4fa7ea58a279c4
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
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Jonathan Bradley Whited
3
+ Copyright (c) 2020-2025 Bradley Whited
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md ADDED
@@ -0,0 +1,305 @@
1
+ # AttrBool
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/attr_bool.svg)](https://badge.fury.io/rb/attr_bool)
4
+ [![Tests Status](https://github.com/esotericpig/attr_bool/actions/workflows/ruby.yml/badge.svg)](https://github.com/esotericpig/attr_bool/actions/workflows/ruby.yml)
5
+ [![Source Code](https://img.shields.io/badge/source-github-%23211F1F.svg)](https://github.com/esotericpig/attr_bool)
6
+ [![Changelog](https://img.shields.io/badge/changelog-md-%23A0522D.svg)](CHANGELOG.md)
7
+ [![License](https://img.shields.io/github/license/esotericpig/attr_bool.svg)](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,146 +1,144 @@
1
1
  # encoding: UTF-8
2
2
  # frozen_string_literal: true
3
3
 
4
- #--
5
- # This file is part of AttrBool.
6
- # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
7
- #
8
- # AttrBool is free software: you can redistribute it and/or modify it under
9
- # the terms of the MIT License.
10
- #
11
- # You should have received a copy of the MIT License along with AttrBool.
12
- # If not, see <https://choosealicense.com/licenses/mit/>.
13
- #++
14
-
15
-
16
4
  require 'bundler/gem_tasks'
17
5
 
6
+ require 'attr_bool/version'
18
7
  require 'benchmark'
19
8
  require 'rake/clean'
20
9
  require 'rake/testtask'
21
- require 'yard'
10
+ require 'rdoc/task'
22
11
 
23
- require 'attr_bool/version'
24
-
25
- CLEAN.exclude('{.git,stock}/**/*')
12
+ CLEAN.exclude('{.git,.github,.idea,stock}/**/*')
26
13
  CLOBBER.include('doc/')
27
14
 
28
- task default: [:test]
15
+ task default: %i[test]
29
16
 
30
- desc 'Generate doc'
31
- task :doc => [:yard] do |task|
32
- end
17
+ desc 'Run all tests'
18
+ task test: %i[test:base test:core_ext]
33
19
 
34
- desc 'Generate doc for tests too'
35
- task :doc_test do |task|
36
- ENV['doctest'] = 'y'
37
-
38
- doc_task = Rake::Task[:doc]
39
-
40
- doc_task.reenable()
41
- doc_task.invoke()
20
+ TEST_DIR = 'test/**'
21
+ CORE_EXT_TEST = 'core_ext_test.rb'
22
+
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
42
35
  end
43
36
 
44
- Rake::TestTask.new() do |task|
45
- task.deps << :doc_test
46
- task.libs = ['lib','test']
47
- task.pattern = File.join('test','**','*_test.rb')
48
- task.description += ": '#{task.pattern}'"
49
- task.verbose = false
50
- task.warning = true
37
+ RDoc::Task.new(:doc) do |t|
38
+ t.rdoc_dir = 'doc'
39
+ t.title = "AttrBool v#{AttrBool::VERSION}"
51
40
  end
52
41
 
53
- YARD::Rake::YardocTask.new() do |task|
54
- task.files = [File.join('lib','**','*.{rb}')]
55
-
56
- #task.options += ['--template-path',File.join('yard','templates')]
57
- task.options += ['--title',"AttrBool v#{AttrBool::VERSION} doc"]
58
-
59
- task.before = Proc.new() do
60
- task.files << File.join('test','**','*.{rb}') if ENV['doctest'].to_s().casecmp?('y')
61
- end
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
62
51
  end
63
52
 
64
- desc 'Benchmark define_method vs module_eval and ?: vs bangbang'
65
- task :benchmark do |task|
66
- N0 = 100_000
67
- N1 = 20_000_000
68
-
69
- module ModuleExt
70
- def do_class_eval(name)
71
- 0.upto(N0) do |i|
72
- n = "#{name}#{i}"
73
-
74
- class_eval("def #{n}?(); @#{n}; end")
75
- end
76
- end
77
-
78
- def do_define_method(name)
79
- 0.upto(N0) do |i|
80
- n = "#{name}#{i}"
81
-
82
- define_method(:"#{n}?") do
83
- instance_variable_get(:"@#{n}")
84
- end
85
- end
86
- end
87
-
88
- def do_module_eval(name)
89
- 0.upto(N0) do |i|
90
- n = "#{name}#{i}"
91
-
92
- module_eval("def #{n}?(); @#{n}; end")
93
- end
94
- end
95
- end
96
-
97
- Module.prepend ModuleExt
98
-
53
+ def bench_def_methods
54
+ # rubocop:disable Style/DocumentDynamicEvalDefinition,Style/EvalWithLocation
55
+
56
+ n = 200_000
57
+
99
58
  puts
100
- Benchmark.bmbm() do |bm|
59
+ Benchmark.bmbm do |bm|
101
60
  bm.report('class_eval ') do
102
- class ClassEvalTest
103
- do_class_eval :ce
61
+ Class.new do
62
+ n.times do |i|
63
+ name = "bool_#{i}"
64
+ class_eval("def #{name}?; @#{name}; end")
65
+ end
104
66
  end
105
67
  end
106
-
68
+
107
69
  bm.report('define_method') do
108
- class DefineMethodTest
109
- do_define_method :dm
70
+ Class.new do
71
+ n.times do |i|
72
+ name = "bool_#{i}"
73
+ define_method(:"#{name}?") { instance_variable_get(:"@#{name}") }
74
+ end
110
75
  end
111
76
  end
112
-
77
+
113
78
  bm.report('module_eval ') do
114
- class ModuleEvalTest
115
- do_module_eval :me
79
+ Class.new do
80
+ n.times do |i|
81
+ name = "bool_#{i}"
82
+ module_eval("def #{name}?; @#{name}; end")
83
+ end
116
84
  end
117
85
  end
118
86
  end
119
-
120
- str = 'str' # Warning workaround
121
-
87
+
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]
96
+
122
97
  puts
123
- Benchmark.bmbm() do |bm|
98
+ Benchmark.bmbm do |bm|
124
99
  bm.report('?:') do
125
- 0.upto(N1) do |i|
126
- x = str ? true : false
127
- x = nil ? true : false
128
- x = true ? true : false
129
- x = false ? true : false
130
- x = x ? true : false
100
+ n.times do
101
+ x = nil
102
+ values.each do |value|
103
+ x = value ? true : false
104
+ end
105
+ x
131
106
  end
132
107
  end
133
-
108
+
134
109
  bm.report('!!') do
135
- 0.upto(N1) do |i|
136
- y = !!str
137
- y = !!nil
138
- y = !!true
139
- y = !!false
140
- y = !!y
110
+ n.times do
111
+ x = nil
112
+ values.each do |value|
113
+ x = !!value
114
+ end
115
+ x
141
116
  end
142
117
  end
143
118
  end
144
-
119
+
145
120
  puts
121
+ Benchmark.bmbm do |bm|
122
+ bm.report('!!') do
123
+ n.times do
124
+ x = nil
125
+ values.each do |value|
126
+ x = !!value
127
+ end
128
+ x
129
+ end
130
+ end
131
+
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
139
+ end
140
+ end
141
+ end
142
+
143
+ # rubocop:enable all
146
144
  end