Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 13, 2018 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/0c2a3f70f68151f7ab022e2430487432 to your computer and use it in GitHub Desktop.
Save havenwood/0c2a3f70f68151f7ab022e2430487432 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class DoNotDump < Module
class << self
alias [] new
end
attr_reader :ignored_ivars
def initialize *ignored_ivars
@ignored_ivars = ignored_ivars.freeze
super() do
define_method :marshal_load do |pairs|
pairs.each { |ivar, value| instance_variable_set ivar, value }
end
define_method :marshal_dump do
instance_variables.each_with_object [] do |ivar, pairs|
pairs << [ivar, instance_variable_get(ivar)] unless ignored_ivars.include? ivar
end
end
end
end
alias to_a ignored_ivars
def == other
other.instance_of?(self.class) && other.ignored_ivars.sort == @ignored_ivars.sort
end
def inspect
"#{self.class.name}#{@ignored_ivars.inspect}"
end
alias to_s inspect
def marshal_dump
@ignored_ivars
end
def marshal_load data
@ignored_ivars = data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment