Sha256: 9d472de88bd56bf93ab0e3f2b92b6884a95e487cf40f3a0bc2eb5c1d15ddd488

Contents?: true

Size: 958 Bytes

Versions: 192

Compression:

Stored size: 958 Bytes

Contents

# frozen_string_literal: true

class Object
  # Returns true if this object is included in the argument. Argument must be
  # any object which responds to +#include?+. Usage:
  #
  #   characters = ["Konata", "Kagami", "Tsukasa"]
  #   "Konata".in?(characters) # => true
  #
  # This will throw an +ArgumentError+ if the argument doesn't respond
  # to +#include?+.
  def in?(another_object)
    another_object.include?(self)
  rescue NoMethodError
    raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
  end

  # Returns the receiver if it's included in the argument otherwise returns +nil+.
  # Argument must be any object which responds to +#include?+. Usage:
  #
  #   params[:bucket_type].presence_in %w( project calendar )
  #
  # This will throw an +ArgumentError+ if the argument doesn't respond to +#include?+.
  #
  # @return [Object]
  def presence_in(another_object)
    in?(another_object) ? self : nil
  end
end

Version data entries

192 entries across 178 versions & 24 rubygems

Version Path
activesupport-7.0.7 lib/active_support/core_ext/object/inclusion.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/core_ext/object/inclusion.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/core_ext/object/inclusion.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/core_ext/object/inclusion.rb
activesupport-7.0.6 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.0.5.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-6.1.7.4 lib/active_support/core_ext/object/inclusion.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/core_ext/object/inclusion.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/core_ext/object/inclusion.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/core_ext/object/inclusion.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/object/inclusion.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/core_ext/object/inclusion.rb
activesupport-7.0.5 lib/active_support/core_ext/object/inclusion.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/core_ext/object/inclusion.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/core_ext/object/inclusion.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/core_ext/object/inclusion.rb
activesupport-7.0.4.3 lib/active_support/core_ext/object/inclusion.rb
activesupport-6.1.7.3 lib/active_support/core_ext/object/inclusion.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/object/inclusion.rb
activesupport-6.1.7.2 lib/active_support/core_ext/object/inclusion.rb