Repository: rails/rails
File: actionpack/lib/action_controller/metal/strong_parameters.rb (Ruby)

Code before:
    end

    # Returns true if another `Parameters` object contains the same content and
    # permitted flag.
    def ==(other)
      if other.respond_to?(:permitted?)
        permitted? == other.permitted? && parameters == other.parameters
      else
        super
      end
    end

    def eql?(other)
      self.class == other.class &&
        permitted? == other.permitted? &&
        parameters.eql?(other.parameters)
    end

    def hash
      [self.class, @parameters, @permitted].hash
    end

    def deconstruct_keys(keys)
      slice(*keys).each.with_object(ActiveSupport::HashWithIndifferentAccess.new) do |(key, value), hash|
        hash.merge!(key => value)
      end
    end

    # Returns a safe ActiveSupport::HashWithIndifferentAccess representation of the
    # parameters with all unpermitted keys removed.
    #
    #     params = ActionController::Parameters.new({
    #       name: "Senjougahara Hitagi",
    #       oddity: "Heavy stone crab"
    #     })
    #     params.to_h
    #     # => ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash
    #
    #     safe_params = params.permit(:name)
    #     safe_params.to_h # => {"name"=>"Senjougahara Hitagi"}
    def to_h(&block)
      if permitted?
        convert_parameters_to_hashes(@parameters, :to_h, &block)
      else
        raise UnfilteredParameters
      end
    end

    # Returns a safe `Hash` representation of the parameters with all unpermitted
    # keys removed.
    #

Review comment (suggestion):
Would this work?
```suggestion
      ActiveSupport::HashWithIndifferentAccess.new(slice(*keys))
```

Code after:
    end

    # Returns true if another `Parameters` object contains the same content and
    # permitted flag.
    def ==(other)
      if other.respond_to?(:permitted?)
        permitted? == other.permitted? && parameters == other.parameters
      else
        super
      end
    end

    def eql?(other)
      self.class == other.class &&
        permitted? == other.permitted? &&
        parameters.eql?(other.parameters)
    end

    def hash
      [self.class, @parameters, @permitted].hash
    end

    def deconstruct_keys(keys)
      slice(*keys).each.with_object({}) { |(key, value), hash| hash.merge!(key.to_sym => value) }
    end

    # Returns a safe ActiveSupport::HashWithIndifferentAccess representation of the
    # parameters with all unpermitted keys removed.
    #
    #     params = ActionController::Parameters.new({
    #       name: "Senjougahara Hitagi",
    #       oddity: "Heavy stone crab"
    #     })
    #     params.to_h
    #     # => ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash
    #
    #     safe_params = params.permit(:name)
    #     safe_params.to_h # => {"name"=>"Senjougahara Hitagi"}
    def to_h(&block)
      if permitted?
        convert_parameters_to_hashes(@parameters, :to_h, &block)
      else
        raise UnfilteredParameters
      end
    end

    # Returns a safe `Hash` representation of the parameters with all unpermitted
    # keys removed.
    #
    #     params = ActionController::Parameters.new({
    #       name: "Senjougahara Hitagi",