In tonight's meeting I tried, and failed, to show how to create a Struct
with a custom method. Turns out, you can't do that. But you can use UPDATE: You can do this. I don't remember what syntax I was trying before, but in 1.9.3 you can successfully pass a block to Class.new
to accomplish this.Struct.new
; though this isn't documented.
The end result is the same:
- A new
Class
object - Default initialize handling the attributes
attr_accessor
methods defined for those attributes
Big thanks to @steveklabnik for his two posts which inspired me to try to show this horribleness (that's a reflection on my silliness, not Steve):
At this point, other than just showing language abuse, I could see this being used in test stubbing and possible as you build out domain concept ideas.