+<sub>This is a response for one of our apprentices who wanted to learn more about how structs work.</sub>

+

+TL;DR

+-----

+

+Part of this is a response and part of it a challenge. If you are only interested in the challenge clone the repo and run rake (more detailed instructions [here](https://gist.github.com/2641441#challenge)).

+

+Introduction

+------------

+

+I use `Struct` in a couple of ways. Doing a quick grep of directories that have made it onto this new computer, there are a couple of ways I use it.

+These are from my gem Surrogate, which helps with hand-rolled mocking. Since `Struct.new` returns a class, I can inherit from it.

+Whatever names I pass to `Struct.new` will become methods that my instances can access. It also defines an initializer for me if I want to use it.

+Note that it is important to use the setters and getters when doing this, rather than instance variables.

+This is a point I explicitly (and, admittedly, rantilly) make one of my other gems, Deject [fourth paragraph](https://github.com/JoshCheek/deject/blob/d781cf016cf0d0ebb17a0997d8899f6ff4d1581e/Readme.md#about-the-code).

+Also notice that I've done this several times in Deject's readme examples.

+

+I usually use this approach to quickly scaffold out a simple class. Usually so simple that it's more of a data structure than an object.

+Meaning its purpose is to hold values rather than encapsulate behaviour -- in general, objects should not have setters,

+because it means you are taking their values and doing things with them or to them, but objects should be declarative interfaces that you interact with,

+not holders of values that you set and get. When these mix, you wind up with a code smell called "feature envy". For more on this, there's a decent blog called "tell, don't ask".

+

+

+As a simple class

+-----------------

+

+Similar to the above, but here the struct becomes the class itself (no need to inherit).