-
-
Save havenwood/4bf28bbb06f601e7013cefb13f6ab455 to your computer and use it in GitHub Desktop.
A Keyword Init Struct Example (Ruby 2.5+)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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