Skip to content

Instantly share code, notes, and snippets.

@zmoazeni
Created December 18, 2014 17:49
Show Gist options
  • Save zmoazeni/e317b78a8c37a2e2fc2a to your computer and use it in GitHub Desktop.
Save zmoazeni/e317b78a8c37a2e2fc2a to your computer and use it in GitHub Desktop.
I've been using this instead of OpenStruct lately
SomeObj = Struct.new(:foo, :bar, :baz) do
def initialize(foo:, bar:, baz: 'is optional')
self.foo = foo
self.bar = bar
self.baz = baz
end
end
p SomeObj.new(foo: 1, bar: 2)
p SomeObj.new(foo: 1, bar: 2, baz: 'overridden')
some_hash = { foo: 'the foo', bar: 'the bar', baz: 'the baz'}
p SomeObj.new(some_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment