Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created May 10, 2018 18:35
Show Gist options
  • Save havenwood/4bf28bbb06f601e7013cefb13f6ab455 to your computer and use it in GitHub Desktop.
Save havenwood/4bf28bbb06f601e7013cefb13f6ab455 to your computer and use it in GitHub Desktop.
A Keyword Init Struct Example (Ruby 2.5+)
Pizza = Struct.new :crust, :sauce, :toppings, keyword_init: true do
def to_s
"#{crust.capitalize} crust with #{sauce} sauce and #{toppings.join ', '}."
end
end
pizza = Pizza.new sauce: 'tomatoe', toppings: %w[mozarella basil], crust: 'golden brown'
#=> #<struct Pizza crust="golden brown", sauce="tomatoe", toppings=["mozarella", "basil"]>
"Order: #{pizza}"
#=> "Order: Golden brown crust with tomatoe sauce and mozarella, basil."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment