Ruby Tips On Struct And OStruct Classes

ScreenHunter 712 Oct. 19 21.27 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)

Related Posts

Optimize Your Site For Retina Display

Active Admin For Ruby On Rails

The Benefits of Using HTML5 in Web Development

Create Interactive Web Videos With Popcorn Maker