Sha256: 5d11da184edf3ab7a25a7fe45ebecd0b78b7ab244ae325df3300df3c8d9b52fd

Contents?: true

Size: 506 Bytes

Versions: 20

Compression:

Stored size: 506 Bytes

Contents

# frozen_string_literal: true

Array.class_eval do
  # Same as `my_array.any?(&block) && !my_array.all?(&block)`
  #
  # @return [Boolean]
  #
  def only_some?(*meth_args)
    # Check arg size since `any?` arg take presence over block
    return any?(*meth_args) && !all?(*meth_args) if meth_args.size.positive?

    any = false
    all = true

    each do |*args|
      result = yield(*args)

      any ||= result
      all &&= result

      break if any && !all
    end

    return any && !all
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

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