Skip to content

Commit 42e6710

Browse files
committed
[GR-45621] Implement Time#deconstruct_keys
PullRequest: truffleruby/4139
2 parents 26a442b + f4157c5 commit 42e6710

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Compatibility:
3838
* Fix `ENV#{clone,dup}` and raise `TypeError` (#3039, @andrykonchin).
3939
* Fix `Coverage.supported?` and raise `TypeError` if argument is not Symbol (#3039, @andrykonchin).
4040
* Accept options argument to `Regexp.{new,compile}` of String and warn for unknown types (#3039, @rwstauner).
41+
* Implement `Time#deconstruct_keys` from Ruby 3.2 (#3039, @rwstauner).
4142

4243
Performance:
4344

spec/ruby/core/time/deconstruct_keys_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
Time.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {}
3838
end
3939

40-
it "ignores not existing Symbol keys" do
41-
Time.new(2022, 10, 5, 13, 30).deconstruct_keys([:year, :a]).should == { year: 2022 }
40+
it "ignores not existing Symbol keys and processes keys after the first non-existing one" do
41+
d = Time.utc(2022, 10, 5, 13, 30)
42+
d.deconstruct_keys([:year, :a, :month, :b, :day]).should == { year: 2022, month: 10, day: 5 }
4243
end
4344
end
4445
end

spec/tags/core/time/deconstruct_keys_tags.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/ruby/truffleruby/core/time.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,42 @@ def ceil(places = 0)
255255
time + (ceiled - original)
256256
end
257257

258+
DEFAULT_DECONSTRUCT_KEYS = %i[year month day yday wday hour min sec subsec dst zone]
259+
260+
def deconstruct_keys(array_of_names)
261+
unless Primitive.nil?(array_of_names) || Primitive.is_a?(array_of_names, Array)
262+
raise TypeError, "wrong argument type #{Primitive.class(array_of_names)} (expected Array or nil)"
263+
end
264+
265+
if Primitive.nil?(array_of_names)
266+
array_of_names = DEFAULT_DECONSTRUCT_KEYS
267+
end
268+
269+
ret = {}
270+
271+
array_of_names.each do |key|
272+
value = case key
273+
when :year then year
274+
when :month then month
275+
when :day then day
276+
when :yday then yday
277+
when :wday then wday
278+
when :hour then hour
279+
when :min then min
280+
when :sec then sec
281+
when :subsec then subsec
282+
when :dst then dst?
283+
when :zone then zone
284+
else
285+
undefined
286+
end
287+
288+
ret[key] = value unless Primitive.undefined?(value)
289+
end
290+
291+
ret
292+
end
293+
258294
def self._load(data)
259295
raise TypeError, 'marshaled time format differ' unless data.bytesize == 8
260296

test/mri/excludes/TestTime.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@
2727
exclude :test_new, "Exception(ArgumentError) with message matches to /invalid value for Integer/."
2828
exclude :test_getlocal_utc_offset, "ArgumentError: \"+HH:MM\" or \"-HH:MM\" expected for utc_offset"
2929
exclude :test_new_from_string, "ArgumentError expected but nothing was raised."
30-
exclude :test_deconstruct_keys, "NoMethodError: undefined method `deconstruct_keys' for 2022-10-16 14:01:30.0005 +0900:Time"

0 commit comments

Comments
 (0)