Sha256: 303188205da148b6e934069c9eb1365a9be14ac0cf492b15909af1389a17ef8d

Contents?: true

Size: 449 Bytes

Versions: 22

Compression:

Stored size: 449 Bytes

Contents

# frozen_string_literal: true

require 'forwardable'
require 'set'

Array.class_eval do
  # @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

Set.class_eval do
  extend Forwardable

  def_delegators :to_a, :compact_map
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
ruby-rails-extensions-2.1.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.14 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.13 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.12 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.11 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.10 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.9 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.8 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.7 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.6 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.5 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.4 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.3 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.2 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.1.0.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.0.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.0.1.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.0.0 lib/ruby-rails-extensions/extensions/compact_map.rb
ruby-rails-extensions-2.0.0.pre.rc.1 lib/ruby-rails-extensions/extensions/compact_map.rb