I thought about the best way to create a ValueObject in Ruby

Conclusion

I think it's best to do the following, but please comment if there is a better way.

module ValueObjectable
  def initialize(args)
    super(args)
    freeze
  end
end

class Location < Struct.new(:country, :city, keyword_init: true)
  include ValueObjectable
end

##Feature##
# 1.Can be initialized with Hash like OpenStruct
tokyo = Location.new(country: 'Japan', city: 'Tokyo')
=> #<struct Location country="Japan", city="Tokyo">

# 2.You don't have to define the comparison logic yourself
fukuoka = Location.new(country: 'Japan', city: 'Fukuoka')
tokyo == fukuoka
=> false

# 3.Value is unchanged
tokyo.country = 'U.S'
FrozenError: can't modify frozen Location

Recommended Posts

I thought about the best way to create a ValueObject in Ruby
I want to create a Parquet file even in Ruby
I want to get the value in Ruby
I tried to create a Clova skill in Java
How to create a placeholder part to use in the IN clause
I want to create a form to select the [Rails] category
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
I tried to create a simple map app in Android Studio
I want to create a chat screen for the Swift chat app!
I tried to illuminate the Christmas tree in a life game
I tried to write code like a type declaration in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
Easy way to create a mapping class when using the API
I thought about the strategy of introducing Combine in iOS development
I want to change the value of Attribute in Selenium of Ruby
(Ruby on Rails6) Create a function to edit the posted content
How to deal with the type that I thought about writing a Java program for 2 years
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
I tried a calendar problem in Ruby
I tried to make a parent class of a value object in Ruby
[Rails] I learned about migration files! (Adding a column to the table)
How to create a query using variables in GraphQL [Using Ruby on Rails]
I wanted to implement a slide show in a fashionable way with slick.
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to make full use of the CPU core in Ruby
A story about converting character codes from UTF-8 to Shift-jis in Ruby
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
[Ruby] Returns characters in a pyramid shape according to the entered numbers
A memorandum to clean up the code Ruby
A story about the JDK in the Java 11 era
I tried to organize the session in Rails
I want to use arrow notation in Ruby
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
Try to create a bulletin board in Java
[Ruby] I want to do a method jump!
I tried to create a LINE clone app
I made a Ruby extension library in C
How to create a theme in Liferay 7 / DXP
How to easily create a pull-down in Rails
Androd: What to do about "The Realm is already in a write transaction in"
I tried to create Alexa skill in Java
How to build the simplest blockchain in Ruby
Create a native extension of Ruby in Rust
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
[Ruby] I want to make a program that displays today's day of the week!
I tried to create a log reproduction script at the time of apt install
[Rails] I want to display the link destination of link_to in a separate tab
Docker command to create Rails project with a single blow in environment without Ruby
The right way to see the tomcat source in eclipse
I was addicted to the NoSuchMethodError in Cloud Endpoints
[Introduction] Try to create a Ruby on Rails application
3. Create a database to access from the web module
How to create a Java environment in just 3 seconds
I tried to organize the cases used in programming
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
I tried to decorate the simple calendar a little
About the difference between classes and instances in Ruby
How to create a Spring Boot project in IntelliJ