Module: AttrBool::Ext
- Defined in:
- lib/attr_bool.rb
Overview
Benchmarks are kind of meaningless, but after playing around with some, I found the following to be the case on my system:
-
define_method
is faster thanmodule_eval
&class_eval
-
? true : false (ternary operator) is faster than !! (surprisingly)
To run benchmark code:
$ bundle exec rake benchmark
Instance Method Summary collapse
- #attr_accessor?(*var_ids, default: nil, reader: nil, writer: nil, &block) ⇒ Boolean
- #attr_bool(*var_ids, default: nil, reader: nil, writer: nil, &block) ⇒ Object (also: #attr_boolor)
- #attr_bool?(*var_ids, default: nil, &block) ⇒ Boolean
- #attr_booler(*var_ids, &block) ⇒ Object
- #attr_reader?(*var_ids, default: nil, &block) ⇒ Boolean
-
#attr_writer?(*var_ids, &block) ⇒ Boolean
This should only be used when you want to pass in a block/proc.
Instance Method Details
#attr_accessor?(*var_ids, default: nil, reader: nil, writer: nil, &block) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/attr_bool.rb', line 32 def attr_accessor?(*var_ids,default: nil,reader: nil,writer: nil,&block) if block reader = block if reader.nil? writer = block if writer.nil? end if default.nil? && reader.nil? last = var_ids[-1] if !last.is_a?(String) && !last.is_a?(Symbol) default = var_ids.pop end end attr_reader?(*var_ids,default: default,&reader) attr_writer?(*var_ids,&writer) end |
#attr_bool(*var_ids, default: nil, reader: nil, writer: nil, &block) ⇒ Object Also known as: attr_boolor
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/attr_bool.rb', line 100 def attr_bool(*var_ids,default: nil,reader: nil,writer: nil,&block) if block reader = block if reader.nil? writer = block if writer.nil? end if default.nil? && reader.nil? last = var_ids[-1] if !last.is_a?(String) && !last.is_a?(Symbol) default = var_ids.pop end end attr_bool?(*var_ids,default: default,&reader) attr_booler(*var_ids,&writer) end |
#attr_bool?(*var_ids, default: nil, &block) ⇒ Boolean
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/attr_bool.rb', line 119 def attr_bool?(*var_ids,default: nil,&block) no_default = default.nil? if no_default no_default = !block if no_default last = var_ids[-1] if !last.is_a?(String) && !last.is_a?(Symbol) default = var_ids.pop ? true : false no_default = false end end else default = default ? true : false end var_ids.each do |var_id| var_id_q = :"#{var_id}?" if no_default define_method(var_id_q) do instance_variable_get(:"@#{var_id}") ? true : false end else if block define_method(var_id_q,&block) else at_var_id = :"@#{var_id}" define_method(var_id_q) do if instance_variable_defined?(at_var_id) instance_variable_get(at_var_id) ? true : false else default end end end end end end |
#attr_booler(*var_ids, &block) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/attr_bool.rb', line 162 def attr_booler(*var_ids,&block) if !block last = var_ids[-1] if !last.is_a?(String) && !last.is_a?(Symbol) raise ArgumentError,'default value not allowed for writer' end end var_ids.each do |var_id| var_id_eq = :"#{var_id}=" if block define_method(var_id_eq,&block) else define_method(var_id_eq) do |value| instance_variable_set(:"@#{var_id}",value ? true : false) end end end end |
#attr_reader?(*var_ids, default: nil, &block) ⇒ Boolean
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/attr_bool.rb', line 50 def attr_reader?(*var_ids,default: nil,&block) no_default = (default.nil? && !block) if no_default last = var_ids[-1] if !last.is_a?(String) && !last.is_a?(Symbol) default = var_ids.pop no_default = false end end var_ids.each do |var_id| var_id_q = :"#{var_id}?" if no_default define_method(var_id_q) do instance_variable_get(:"@#{var_id}") end else if block define_method(var_id_q,&block) else at_var_id = :"@#{var_id}" define_method(var_id_q) do instance_variable_defined?(at_var_id) ? instance_variable_get(at_var_id) : default end end end end end |
#attr_writer?(*var_ids, &block) ⇒ Boolean
This should only be used when you want to pass in a block/proc.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/attr_bool.rb', line 84 def attr_writer?(*var_ids,&block) if block var_ids.each do |var_id| define_method(:"#{var_id}=",&block) end else last = var_ids[-1] if !last.is_a?(String) && !last.is_a?(Symbol) raise ArgumentError,'default value not allowed for writer' end attr_writer(*var_ids) end end |