Hi, thanks for taking the time to read my article. Visit aaronvb.com for more reads, and contact me below if you have questions.


You can find me on GitHub, Twitter, Instagram, Flickr, or email at bokhoven@gmail.com

Ruby Hash to Class Object and OpenStruct

Written Aaron Van Bokhoven on Jul 12, 2010

I’ve been working on a class that’s pulling information from an API and wanted to create dynamic attributes from a hash(json).

I came across this link:


It works pretty well, but I haven’t done much benchmarking. I’ve read in the past that define_method can be slow and has memory leaks.

Following that post, I read the comments and came across OpenStruct:


I have it working with what I need but there’s still more testing to be done. Just thought I would share.