Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created October 11, 2018 04:20
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/ff1989451fbb0d48b63a1c9ddf22c325 to your computer and use it in GitHub Desktop.
Save havenwood/ff1989451fbb0d48b63a1c9ddf22c325 to your computer and use it in GitHub Desktop.
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