Sha256: 314fe21b95dbe72e82753722433f992423914a4b38177e154e2bfbbcdab57c90

Contents?: true

Size: 565 Bytes

Versions: 9

Compression:

Stored size: 565 Bytes

Contents

# frozen_string_literal: true

require 'forwardable'
require 'set'

Array.class_eval do
  unless method_defined?(:compact_map)
    # @return [Array]
    # Similar to `filter_map` but excludes `nil` instead of falsy
    def compact_map
      return to_enum(:compact_map) unless block_given?

      r = []
      each do |*args|
        result = yield(*args)
        r.push(result) unless result.nil?
      end
      r
    end
  end
end

Set.class_eval do
  unless method_defined?(:compact_map)
    extend Forwardable

    def_delegators :to_a, :compact_map
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ruby-rails-extensions-1.1.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-1.1.1.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-0.2.0 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-0.2.0.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-1.1.0 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-1.1.0.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-0.1.0 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-1.0.0 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-1.0.0.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb