Skip to content

Instantly share code, notes, and snippets.

@michaelglass
Created June 1, 2017 14:43
Show Gist options
  • Save michaelglass/b702603f4d02ffbd16cfc884896e44dd to your computer and use it in GitHub Desktop.
Save michaelglass/b702603f4d02ffbd16cfc884896e44dd to your computer and use it in GitHub Desktop.
class Value
# alias_method is private, wah wah
singleton_class.send(:alias_method, :old_new, :new)
def self.new(*fields, &block)
klass = old_new(*fields, &block)
klass.instance_eval do
# Rails' as_json function calls `to_hash` internally and falls back to using `instance_variables`.
# `instance_variables` returns Value's internal `hash` attribute which we are not interested in.
alias_method :to_hash, :to_h
end
klass
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment