Skip to content

Instantly share code, notes, and snippets.

@tpitale
Created August 5, 2013 01:54
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 tpitale/6152948 to your computer and use it in GitHub Desktop.
Save tpitale/6152948 to your computer and use it in GitHub Desktop.
Give Ruby 1.9 OpenStruct [] and []= like in Ruby 2.0
options = OpenStruct.new({a: 1, b: 2})
# make options[] work in 1.9 like 2.0
unless options.respond_to?(:[])
options.extend(Forwardable)
options.def_delegators :@table, :[], :[]=
end
# now I can do
options[:a]
# instead of having to do
options.send(:a)
# but can still do
options.a
options.b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment