Open structs create new objects while a Struct defines a class that already has attributes, an equality method (==), and is enumerable.
require 'ostruct' car1 = OpenStruct.new(type: "car", color: "red") car2 = OpenStruct.new(name: "car", color: "red") car3 = OpenStruct.new(name: "car", color: "blue") car1 == car2 # => true car1 == car3 # => false
While you can’t define a method on and OpenStruct, one advantage they have is that you can add a new attribute to it after it has been defined. You can’t do this with a Struct.
require 'ostruct' car = OpenStruct.new(type: "Toyota") car.type # => "Toyota" car.mpg # => nil car.mpg = 30 car.mpg # => 30