I need a little bit of refactor my initialize method because I think it will make initializer more flexible and readable.

class LogAdminData

  def initialize(admin_obj:, action_type:, old_data:, new_data:, excluded_params: %w[])
    excluded_params += DEFAULT_EXCLUDED_PARAMS
    @old_data = old_data.reject { |k, _v| excluded_params.include? k }
    @new_data = new_data.reject { |k, _v| excluded_params.include? k }
    @action_type = action_type
    @admin_email = admin_obj.email
    @admin_role = admin_obj.role

  def call
      admin_email: admin_email,
      admin_role: admin_role,
      action_type: action_type,
      new_data: new_data,
      old_data: old_data,

Maybe something like

  def cleanup_data(data)
    excluded_params += DEFAULT_EXCLUDED_PARAMS
    data.reject { |k, _v| excluded_params.include? k }

But how to call it in initializer using old_data and new_data?


closed as unclear what you're asking by 200_success, dfhwze, Heslacher, AlexV, Dannnno Sep 12 at 14:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ What does this code do, and how is it used? Is this the entire class? As it is, it looks like some essential parts of the code are missing, and your question makes little sense. \$\endgroup\$ – 200_success Sep 12 at 3:24

If this is a Rails project (or you use ActiveSupport) you can replace

@old_data = old_data.reject { |k, _v| excluded_params.include? k } with @old_data = old_data.except(excluded_params)


Not the answer you're looking for? Browse other questions tagged or ask your own question.