Sha256: 6574345ef841d8faa6d3f1dd6b3c44d439b826cc317c70426d50e32a7ffe47b1
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
module Jinx # A Flattener applies a given block to flattened collection content. class Flattener include Enumerable, Collection # Visits the enumerated items in the given object's flattened content. # The given block is called on the base itself if the base is neither nil nor a Enumerable. # If the base object is nil or empty, then this method is a no-op and returns nil. def self.on(obj, &block) obj.collection? ? obj.each { |item| on(item, &block) } : yield(obj) unless obj.nil? end # Initializes a new Flattener on the given object. # # @param obj the Enumerable or non-collection object def initialize(obj) @base = obj end # Calls the the given block on this Flattener's flattened content. # If the base object is a collection, then the block is called on the flattened content. # If the base object is nil, then this method is a no-op. # If the base object is neither nil nor a collection, then the block given to this method # is called on the base object itself. # # @example # Flattener.new(nil).each { |n| print n } #=> # Flattener.new(1).each { |n| print n } #=> 1 # Flattener.new([1, [2, 3]]).each { |n| print n } #=> 123 def each(&block) Flattener.on(@base, &block) end def to_s to_a.to_s end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jinx-2.1.4 | lib/jinx/helpers/flattener.rb |