Skip to content
Justice Tech School

Struct

Struct makes a small class with accessors in one line. Data makes immutable ones.

Example
P = Struct.new(:name, :age)
p1 = P.new("Ada", 30)
p1.age += 1
puts p1, p1.to_a.inspect
Pt = Data.define(:x, :y)
puts Pt.new(1, 2).x

Result

#<struct P name="Ada", age=31>
["Ada", 31]
1

Exercise

Quick class.

.new(:a)

Quiz Test

Is Data mutable?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.