Skip to content

Commit

Permalink
Raise ArgumentError when an invalid form is passed to Date#to_time
Browse files Browse the repository at this point in the history
Before this commit
`NoMethodError: undefined method `form_name' for Time:Class` is raised
when an invalid argument is passed.
It is better to raise `ArgumentError` and show list of valid arguments
to developers.
  • Loading branch information
yui-knk committed Apr 16, 2016
1 parent 91798c7 commit 29e876f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def readable_inspect
#
# date.to_time(:utc) # => 2007-11-10 00:00:00 UTC
def to_time(form = :local)
raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
::Time.send(form, year, month, day)
end

Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/date_ext_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def test_to_time
end
end
end

assert_raise(ArgumentError) do
Date.new(2005, 2, 21).to_time(:tokyo)
end
end

def test_compare_to_time
Expand Down

0 comments on commit 29e876f

Please sign in to comment.