Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active October 12, 2018 09:53
Show Gist options
  • Save havenwood/ae6fe35b5d006358c81ad6c3b99c6b21 to your computer and use it in GitHub Desktop.
Save havenwood/ae6fe35b5d006358c81ad6c3b99c6b21 to your computer and use it in GitHub Desktop.
class DoNotDump < Module
class << self
alias [] new
end
def initialize *ignored_ivars
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
end
class Example
def initialize
@unserializable = ->{}
end
end
Marshal.load Marshal.dump Example.new
#!> TypeError: no _dump_data is defined for class Proc
class Example
include DoNotDump[:@unserializable]
end
Marshal.load Marshal.dump Example.new
#=> #<Example:...>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment