Skip to content

Commit a14a1a5

Browse files
committed
[Feature #19163] Data object should be frozen
1 parent 06a0c58 commit a14a1a5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

struct.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1818,9 +1818,19 @@ rb_data_initialize_m(int argc, const VALUE *argv, VALUE self)
18181818
if (arg.unknown_keywords != Qnil) {
18191819
rb_exc_raise(rb_keyword_error_new("unknown", arg.unknown_keywords));
18201820
}
1821+
OBJ_FREEZE_RAW(self);
18211822
return Qnil;
18221823
}
18231824

1825+
/* :nodoc: */
1826+
static VALUE
1827+
rb_data_init_copy(VALUE copy, VALUE s)
1828+
{
1829+
copy = rb_struct_init_copy(copy, s);
1830+
RB_OBJ_FREEZE_RAW(copy);
1831+
return copy;
1832+
}
1833+
18241834
/*
18251835
* call-seq:
18261836
* inspect -> string
@@ -2180,7 +2190,7 @@ InitVM_Struct(void)
21802190
#endif
21812191

21822192
rb_define_method(rb_cData, "initialize", rb_data_initialize_m, -1);
2183-
rb_define_method(rb_cData, "initialize_copy", rb_struct_init_copy, 1);
2193+
rb_define_method(rb_cData, "initialize_copy", rb_data_init_copy, 1);
21842194

21852195
rb_define_method(rb_cData, "==", rb_data_equal, 1);
21862196
rb_define_method(rb_cData, "eql?", rb_data_eql, 1);

test/ruby/test_data.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_initialize
6262
assert_equal(1, test.foo)
6363
assert_equal(2, test.bar)
6464
assert_equal(test, klass.new(1, 2))
65+
assert_predicate(test, :frozen?)
6566

6667
# Keywords
6768
test_kw = klass.new(foo: 1, bar: 2)
@@ -169,4 +170,11 @@ def test_memberless
169170
assert_equal([], test.members)
170171
assert_equal({}, test.to_h)
171172
end
173+
174+
def test_dup
175+
klass = Data.define(:foo, :bar)
176+
test = klass.new(foo: 1, bar: 2)
177+
assert_equal(klass.new(foo: 1, bar: 2), test.dup)
178+
assert_predicate(test.dup, :frozen?)
179+
end
172180
end

0 commit comments

Comments
 (0)