Skip to content

Commit

Permalink
Implement Time#deconstruct_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
rwstauner committed Jan 25, 2024
1 parent 394532f commit 5aeb599
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Compatibility:
* Implement `MatchData#{byteoffset,deconstruct,deconstruct_keys}` from Ruby 3.2 (#3039, @rwstauner).
* Add `Integer#ceildiv` method (#3039, @simonlevasseur, @nirvdrum).
* Implement `Class#attached_object` method (#3039, @andrykonchin).
* Implement `Time#deconstruct_keys` from Ruby 3.2 (@rwstauner).

Performance:

Expand Down
5 changes: 3 additions & 2 deletions spec/ruby/core/time/deconstruct_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
Time.new(2022, 10, 5, 13, 30).deconstruct_keys(['year', []]).should == {}
end

it "ignores not existing Symbol keys" do
Time.new(2022, 10, 5, 13, 30).deconstruct_keys([:year, :a]).should == { year: 2022 }
it "ignores not existing Symbol keys and processes keys after the first non-existing one" do
d = Time.utc(2022, 10, 5, 13, 30)
d.deconstruct_keys([:year, :a, :month, :b, :day]).should == { year: 2022, month: 10, day: 5 }
end
end
end
7 changes: 0 additions & 7 deletions spec/tags/core/time/deconstruct_keys_tags.txt

This file was deleted.

43 changes: 43 additions & 0 deletions src/main/ruby/truffleruby/core/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,49 @@ def ceil(places = 0)
time + (ceiled - original)
end

DEFAULT_DECONSTRUCT_KEYS = %i[year month day yday wday hour min sec subsec dst zone]

def deconstruct_keys(array_of_names)
unless Primitive.nil?(array_of_names) || Primitive.is_a?(array_of_names, Array)
raise TypeError, "wrong argument type #{Primitive.class(array_of_names)} (expected Array or nil)"
end

if Primitive.nil?(array_of_names)
array_of_names = DEFAULT_DECONSTRUCT_KEYS
end

ret = {}

array_of_names.each do |key|
case key
when :year
ret[:year] = year
when :month
ret[:month] = month
when :day
ret[:day] = day
when :yday
ret[:yday] = yday
when :wday
ret[:wday] = wday
when :hour
ret[:hour] = hour
when :min
ret[:min] = min
when :sec
ret[:sec] = sec
when :subsec
ret[:subsec] = subsec
when :dst
ret[:dst] = dst?
when :zone
ret[:zone] = zone
end
end

ret
end

def self._load(data)
raise TypeError, 'marshaled time format differ' unless data.bytesize == 8

Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestTime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@
exclude :test_new, "Exception(ArgumentError) with message matches to /invalid value for Integer/."
exclude :test_getlocal_utc_offset, "ArgumentError: \"+HH:MM\" or \"-HH:MM\" expected for utc_offset"
exclude :test_new_from_string, "ArgumentError expected but nothing was raised."
exclude :test_deconstruct_keys, "NoMethodError: undefined method `deconstruct_keys' for 2022-10-16 14:01:30.0005 +0900:Time"

0 comments on commit 5aeb599

Please sign in to comment.