Sha256: eb3fdd73a6f05fb2cd2b8cca1b841405fc806921566dca5276a6cbe198c7fabe

Contents?: true

Size: 1.17 KB

Versions: 43

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

class Object
  # Returns true if this object is included in the argument.
  #
  # When argument is a +Range+, +#cover?+ is used to properly handle inclusion
  # check within open ranges. Otherwise, argument must be any object which responds
  # to +#include?+. Usage:
  #
  #   characters = ["Konata", "Kagami", "Tsukasa"]
  #   "Konata".in?(characters) # => true
  #
  # For non +Range+ arguments, this will throw an +ArgumentError+ if the argument
  # doesn't respond to +#include?+.
  def in?(another_object)
    case another_object
    when Range
      another_object.cover?(self)
    else
      another_object.include?(self)
    end
  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

43 entries across 43 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/object/inclusion.rb
activesupport-8.0.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-8.0.0.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.2.2.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.1.5.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-8.0.0 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.2.2 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.1.5 lib/active_support/core_ext/object/inclusion.rb
activesupport-8.0.0.rc2 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.2.1.2 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.1.4.2 lib/active_support/core_ext/object/inclusion.rb
activesupport-8.0.0.rc1 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.2.1.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-7.1.4.1 lib/active_support/core_ext/object/inclusion.rb
activesupport-8.0.0.beta1 lib/active_support/core_ext/object/inclusion.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/core_ext/object/inclusion.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/core_ext/object/inclusion.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/core_ext/object/inclusion.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/core_ext/object/inclusion.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/core_ext/object/inclusion.rb