Skip to content

Commit ef6f6dc

Browse files
committed
fixup! Implement Date#deconstruct_keys and DateTime#deconstruct_keys
1 parent c4708bb commit ef6f6dc

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

ext/date/date_core.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7438,14 +7438,12 @@ d_lite_jisx0301(VALUE self)
74387438

74397439
static VALUE
74407440
deconstruct_keys(VALUE self, VALUE keys, int is_datetime) {
7441-
VALUE h;
7441+
VALUE h = rb_hash_new();
74427442
long i;
74437443

74447444
get_d1(self);
74457445

74467446
if (NIL_P(keys)) {
7447-
h = rb_hash_new();
7448-
74497447
rb_hash_aset(h, sym_year, m_real_year(dat));
74507448
rb_hash_aset(h, sym_month, INT2FIX(m_mon(dat)));
74517449
rb_hash_aset(h, sym_day, INT2FIX(m_mday(dat)));
@@ -7468,8 +7466,6 @@ deconstruct_keys(VALUE self, VALUE keys, int is_datetime) {
74687466

74697467
}
74707468

7471-
h = rb_hash_new();
7472-
74737469
for (i=0; i<RARRAY_LEN(keys); i++) {
74747470
VALUE key = RARRAY_AREF(keys, i);
74757471

test/date/test_date.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,27 @@ def test_deconstruct_keys
180180
d = Date.new(1999,5,23)
181181
assert_equal({year: 1999, month: 5, day: 23, wday: 0, yday: 143}, d.deconstruct_keys(nil))
182182
assert_equal({year: 1999}, d.deconstruct_keys([:year, :century]))
183+
assert_equal(
184+
{year: 1999, month: 5, day: 23, wday: 0, yday: 143},
185+
d.deconstruct_keys([:year, :month, :day, :wday, :yday])
186+
)
183187

184188
dt = DateTime.new(1999, 5, 23, 4, 20, Rational(1, 10000))
185189

186190
assert_equal(
187191
{year: 1999, month: 5, day: 23, wday: 0, yday: 143,
188-
hour: 4, min: 20, sec: 13, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"},
192+
hour: 4, min: 20, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"},
189193
dt.deconstruct_keys(nil)
190194
)
191195

192196
assert_equal({year: 1999}, dt.deconstruct_keys([:year, :century]))
193197

198+
assert_equal(
199+
{year: 1999, month: 5, day: 23, wday: 0, yday: 143,
200+
hour: 4, min: 20, sec: 0, sec_fraction: Rational(1, 10000), zone: "+00:00"},
201+
dt.deconstruct_keys([:year, :month, :day, :wday, :yday, :hour, :min, :sec, :sec_fraction, :zone])
202+
)
203+
194204
dtz = DateTime.parse('3rd Feb 2001 04:05:06+03:30')
195205
assert_equal({zone: '+03:30'}, dtz.deconstruct_keys([:zone]))
196206
end

0 commit comments

Comments
 (0)