-
-
Save havenwood/ff1989451fbb0d48b63a1c9ddf22c325 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 | |
@ignored = -> ivar { ignored_ivars.include? ivar } | |
end | |
def included base | |
base.class_exec @ignored do |ignored| | |
@@ignored = ignored | |
def marshal_load data | |
Marshal.load(data).each { |ivar, value| instance_variable_set ivar, value } | |
end | |
def marshal_dump | |
data = instance_variables.grep_v(@@ignored).map do |ivar| | |
[ivar, instance_variable_get(ivar)] | |
end | |
Marshal.dump data | |
end | |
end | |
end | |
end | |
class Example | |
def initialize | |
@unserializable = ->{} | |
end | |
end | |
class Example | |
include DoNotDump[:@unserializable] | |
end | |
p Marshal.load Marshal.dump Example.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment