Class: Array
Instance Method Summary collapse
-
#count_by(method) ⇒ Hash
Count occurrences by method result using tally (Ruby 2.7+).
-
#deref(count = 4) ⇒ Object
trim the backtrace to project source files exclude vendor and .bundle directories limit the count to 4 replace the home directory with a .
-
#filter_map_by(method, value, transform_method = nil) ⇒ Array
Use filter_map for combined filtering and mapping (Ruby 2.7+) Filters elements and transforms them in one pass.
-
#find_by(method = nil, value = nil, default = nil, &block) ⇒ Object?
Find the first element where the specified method matches a value Supports both method-based and block-based filtering.
-
#find_where(conditions = {}) ⇒ Array
Find elements using hash-based conditions All conditions must match for an element to be included.
-
#match_by(method, pattern) ⇒ Array
Match elements using pattern matching (Ruby 2.7+) Uses grep for pattern matching against method results.
-
#partition_by(method, value) ⇒ Array
Partition elements based on method result.
- #pluck(key) ⇒ Object
-
#process_and_conditionally_delete! ⇒ Array
Processes each element of the array, yielding the previous, current, and next elements to the given block.
-
#reject_by(method = nil, value = nil, &block) ⇒ Array
Reject elements where the specified method matches a value Supports both method-based and block-based filtering.
-
#select_by(method = nil, value = nil, &block) ⇒ Array
Select elements where the specified method matches a value Supports both method-based and block-based filtering.
-
#select_where(conditions = {}) ⇒ Array
Select elements using hash-based conditions All conditions must match for an element to be included.
Instance Method Details
#count_by(method) ⇒ Hash
Count occurrences by method result using tally (Ruby 2.7+)
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/ww.rb', line 247 def count_by(method) raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) if respond_to?(:tally) # Ruby 2.7+ map do |item| item.respond_to?(method) ? item.send(method) : nil end.compact.tally else # Fallback for older Ruby versions result = Hash.new(0) each do |item| if item.respond_to?(method) value = item.send(method) result[value] += 1 end end result end rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" {} end |
#deref(count = 4) ⇒ Object
trim the backtrace to project source files exclude vendor and .bundle directories limit the count to 4 replace the home directory with a .
222 223 224 225 226 227 228 229 |
# File 'lib/ww.rb', line 222 def deref(count = 4) dir_pwd = Dir.pwd map(&:deref).reject do |line| %r{^/(vendor|\.bundle)/}.match(line) end.first(count).map do |line| line.sub(dir_pwd, '.') end end |
#filter_map_by(method, value, transform_method = nil) ⇒ Array
Use filter_map for combined filtering and mapping (Ruby 2.7+) Filters elements and transforms them in one pass
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/ww.rb', line 279 def filter_map_by(method, value, transform_method = nil) raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) if respond_to?(:filter_map) # Ruby 2.7+ filter_map do |item| if item.respond_to?(method) && item.send(method) == value transform_method ? item.send(transform_method) : item end end else # Fallback for older Ruby versions result = [] each do |item| if item.respond_to?(method) && item.send(method) == value result << (transform_method ? item.send(transform_method) : item) end end result end rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" [] end |
#find_by(method = nil, value = nil, default = nil, &block) ⇒ Object?
Find the first element where the specified method matches a value Supports both method-based and block-based filtering
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/ww.rb', line 312 def find_by(method = nil, value = nil, default = nil, &block) if block_given? find(&block) else raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) find do |item| item.respond_to?(method) && item.send(method) == value end || default end rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" default end |
#find_where(conditions = {}) ⇒ Array
Find elements using hash-based conditions All conditions must match for an element to be included
333 334 335 336 337 338 339 340 341 342 |
# File 'lib/ww.rb', line 333 def find_where(conditions = {}) find do |item| conditions.all? do |method, value| item.respond_to?(method) && item.send(method) == value end end rescue NoMethodError => err warn "Some methods not available on items: #{err.}" nil end |
#match_by(method, pattern) ⇒ Array
Match elements using pattern matching (Ruby 2.7+) Uses grep for pattern matching against method results
350 351 352 353 354 355 356 357 358 |
# File 'lib/ww.rb', line 350 def match_by(method, pattern) raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) grep { |item| pattern === item.send(method) } rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" [] end |
#partition_by(method, value) ⇒ Array
Partition elements based on method result
365 366 367 368 369 370 371 372 373 |
# File 'lib/ww.rb', line 365 def partition_by(method, value) raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) partition { |item| item.respond_to?(method) && item.send(method) == value } rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" [[], self] end |
#pluck(key) ⇒ Object
6 7 8 |
# File 'lib/array.rb', line 6 def pluck(key) map { |hash| hash[key] if hash.is_a?(Hash) }.compact end |
#process_and_conditionally_delete! ⇒ Array
Processes each element of the array, yielding the previous, current, and next elements to the given block. Deletes the current element if the block returns true.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/array.rb', line 13 def process_and_conditionally_delete! i = 0 while i < length prev_item = self[i - 1] unless i.zero? current_item = self[i] next_item = self[i + 1] should_delete = yield prev_item, current_item, next_item if should_delete delete_at(i) else i += 1 end end self end |
#reject_by(method = nil, value = nil, &block) ⇒ Array
Reject elements where the specified method matches a value Supports both method-based and block-based filtering
382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/ww.rb', line 382 def reject_by(method = nil, value = nil, &block) if block_given? reject(&block) else raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) reject { |item| item.respond_to?(method) && item.send(method) == value } end rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" self end |
#select_by(method = nil, value = nil, &block) ⇒ Array
Select elements where the specified method matches a value Supports both method-based and block-based filtering
403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/ww.rb', line 403 def select_by(method = nil, value = nil, &block) if block_given? select(&block) else raise ArgumentError, 'Method must be a Symbol or String' unless method.is_a?(Symbol) || method.is_a?(String) select { |item| item.respond_to?(method) && item.send(method) == value } end rescue NoMethodError => err warn "Method #{method} not available on some items: #{err.}" [] end |
#select_where(conditions = {}) ⇒ Array
Select elements using hash-based conditions All conditions must match for an element to be included
422 423 424 425 426 427 428 429 430 431 |
# File 'lib/ww.rb', line 422 def select_where(conditions = {}) select do |item| conditions.all? do |method, value| item.respond_to?(method) && item.send(method) == value end end rescue NoMethodError => err warn "Some methods not available on items: #{err.}" [] end |