Ruby Tips On Struct And OStruct Classes
Steve Klabnik wrote an interesting piece on how to take make the most of Ruby’s classes, which are the Struct class and OpenStruct class. In his blog post, Steve shares its documentation and gives a few good examples of where and when to take advantage of these structs and use them quite effectively.
For instance, using Struct.new gives you a Class.
From this:
class Point < Struct.new(:x, :y)
end
origin = Point.new(0,0)
To this:
1.9.3p194 :001 > Struct.new(:x,:y)
=> #<Class:0x007f8fc38da2e8>
Although making an empty class may look like this:
Point = Struct.new(:x, :y)
origin = Point(0,0)