In this example we can see how to pass a block to a Struck for a default value.

Course = Struct.new :title, :major, :location do
  def initialize(*)
    super
    self.location ||= 'UCLA'
  end
end

course = Course.new("Machine Learning", 'Comp Sci')
=> # 

course = Person.new("Machine Learning", 'Comp Sci', "UCSD")
=> #

Comments

    • Fred February 23, 2014
    • Reply

    Small typo, self.location should be self.university

    • Brett February 23, 2014
    • Reply

    Thanks, switched university for location in the creation of the struct.

Leave a Reply

Screen Shot 2013-12-08 at 9.49.35 PM

I have helped a dozen companies build and optimize their engineering teams to deliver great products.

Let’s work together and I can align your business needs with your technology initiatives.