rubylearning.com
06 Sep '12, 2am
All about Struct
This guest post is by Steve Klabnik . Steve is a Rubyist, writer, and teaches Ruby and Rails classes with Jumpstart Lab. He maintains Draper, Hackety Hack, and Shoes, and contributes to Rails from time to time. O ne of my favorite classes in Ruby is Struct , but I feel like many Rubyists don’t know when to take advantage of it. The standard library has a lot of junk in it, but Struct and OStruct are super awesome. Struct If you haven’t used Struct before, here’s the documentation of Struct from the Ruby standard library . Structs are used to create super simple classes with some instance variables and a simple constructor. Check it: Struct.new("Point", :x, :y) #=> Struct::Point origin = Struct::Point.new(0,0) #=> # Nobody uses it this way, though. Here’s the way I first saw it used: class Point < Struct.new(:x, :y) end origin = Point.new(0,0) Wait, what? Inherit…from an in...
Full article:
http://rubylearning.com/blog/2012/09/06/all-about-struct/