Skip to content

Instantly share code, notes, and snippets.

@minghz
Created April 17, 2020 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minghz/bf3ca50a59d177a70c19e985740a18b9 to your computer and use it in GitHub Desktop.
Save minghz/bf3ca50a59d177a70c19e985740a18b9 to your computer and use it in GitHub Desktop.
Three Examples of DTOs in Ruby - Method 1: Simple Immutable Object
#
# In this example, all required parameters must be provided during
# construction of the object, or it will fail to be created.
#
# The attributes can be read but not modified after object construction.
#
class BirthdayCard
attr_reader :from, :to, :messages
def initialize(from:, to:, messages:)
@from = from
@to = to
@messages = messages
end
end
# To create this DTO
BirthdayCard.new(from: 'me',
to: 'you',
messages: ['Wishing you a Happy one!',
'You are getting old!'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment