Last active
October 12, 2018 09:53
-
-
Save havenwood/ae6fe35b5d006358c81ad6c3b99c6b21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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