Ruby Data Class: Syntax

What is Ruby’s Data class?

The Data class was introduced to Ruby in Ruby 3.2, to:

‘define simple classes for value-alike’
store immutable atomic values

Create the blueprint (instance of Data class)

We call Data.define and pass…


This content originally appeared on DEV Community and was authored by Kelly Popko

What is Ruby's Data class?

The Data class was introduced to Ruby in Ruby 3.2, to:

  • 'define simple classes for value-alike'
  • store immutable atomic values

Create the blueprint (instance of Data class)

We call Data.define and pass keyword arguments.

Book = Data.define(:title, :author, :year)

We can define methods as well if we pass it a block.

Book = Data.define(:title, :author, :year) do
  SUMMARY = '%<title>s was written by %<author>s in %<year>i.'

  def to_s
    SUMMARY % {title:, author:, year:}
  end
end
hobbit = Book.new("The Hobbit", "J.R.R. Tolkien", 1937)
puts hobbit
# => nil
'The Hobbit was written by J.R.R. Tolkien in 1937.'

Building from the blueprint

  • Call .new or use [] notation
  • Use keyword or positional arguments

.new + keyword arguments

hobbit = Book.new(title: "The Hobbit", author: "J. R. R. Tolkien", year: 1937)
# => <data Book title="The Hobbit", author="J. R. R. Tolkien", year=1937>

hobbit.author
# => "J. R. R. Tolkien"

hobbit.year
# => 1937

hobbit.title
# => "The Hobbit"

Bracket Notation + Positional arguments

Location = Data.define(:latitude, :longitude)
# => Location

We can use positional arguments but need to take care with the order.

philadelphia = Location[39.9526, -75.1652]
# => #<data Location latitude=39.9526, longitude=-75.1652>

philadelphia.latitude
# => 39.9526

philadelphia = Location[-75.1652, 39.9526]
# => #<data Location latitude=-75.1652, longitude=39.9526>

philadelphia.latitude
# => -75.1652

Bracket Notation + Keyword Arguments

Note that with keyword arguments, we have flexibility in the order we pass arguments.

philadelphia = Location[latitude: 39.9526, longitude:-75.1652]
# => #<data Location latitude=39.9526, longitude=-75.1652>

philadelphia.latitude
# => 39.9526
philadelphia = Location[longitude:-75.1652, latitude: 39.9526]
# => #<data Location latitude=39.9526, longitude=-75.1652>

philadelphia.latitude
# => 39.9526

Further Reading


This content originally appeared on DEV Community and was authored by Kelly Popko


Print Share Comment Cite Upload Translate Updates
APA

Kelly Popko | Sciencx (2025-07-21T23:04:47+00:00) Ruby Data Class: Syntax. Retrieved from https://www.scien.cx/2025/07/21/ruby-data-class-syntax/

MLA
" » Ruby Data Class: Syntax." Kelly Popko | Sciencx - Monday July 21, 2025, https://www.scien.cx/2025/07/21/ruby-data-class-syntax/
HARVARD
Kelly Popko | Sciencx Monday July 21, 2025 » Ruby Data Class: Syntax., viewed ,<https://www.scien.cx/2025/07/21/ruby-data-class-syntax/>
VANCOUVER
Kelly Popko | Sciencx - » Ruby Data Class: Syntax. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/21/ruby-data-class-syntax/
CHICAGO
" » Ruby Data Class: Syntax." Kelly Popko | Sciencx - Accessed . https://www.scien.cx/2025/07/21/ruby-data-class-syntax/
IEEE
" » Ruby Data Class: Syntax." Kelly Popko | Sciencx [Online]. Available: https://www.scien.cx/2025/07/21/ruby-data-class-syntax/. [Accessed: ]
rf:citation
» Ruby Data Class: Syntax | Kelly Popko | Sciencx | https://www.scien.cx/2025/07/21/ruby-data-class-syntax/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.