

NameError: undefined method ‘parse’ for class ‘NilClass’ when doing Time.zone.pa...
source link: https://mensfeld.pl/2019/10/nameerror-undefined-method-parse-for-class-nilclass-when-doing-time-zone-parse/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

If you get following error when trying to parse time:
Time
.zone.parse(
'2019-01-01 11:11:11'
)
Traceback (most recent call last):
16
: from /bundler/friendly_errors.rb:
124
:in
`with_friendly_errors'
15
: from /bundle:
30
:in
`block
in
<top (required)>'
14
: from /bundler/cli.rb:
18
:in
`start'
13
: from /bundler/vendor/thor/lib/thor/base.rb:
466
:in
`start'
12
: from /bundler/cli.rb:
27
:in
`dispatch'
11
: from /bundler/vendor/thor/lib/thor.rb:
387
:in
`dispatch'
10
: from /bundler/vendor/thor/lib/thor/invocation.rb:
126
:in
`invoke_command'
9
: from /bundler/vendor/thor/lib/thor/command.rb:
27
:in
`run'
8
: from /bundler/cli.rb:
465
:in
`exec'
7
: from /bundler/cli/exec.rb:
28
:in
`run'
6
: from /bundler/cli/exec.rb:
74
:in
`kernel_load'
5
: from /bundler/cli/exec.rb:
74
:in
`load'
4
: from bin/irb:
23
:in
`<top (required)>'
3
: from bin/irb:
23
:in
`load'
2
: from lib/ruby/gems/
2
.
6
.
0
/gems/irb-
1
.
0
.
0
/exe/irb:
11
:in
`<top (required)>'
1
: from (irb):
5
NoMethodError (undefined method `parse'
for
nil
:
NilClass
)
it means you’ve forgotten to set the timezone:
Time
.zone =
'UTC'
Also, keep in mind, that each time you spin up a new thread, it won’t have a timezone defined (outside of Rails):
Time
.zone =
'UTC'
Time
.zone.parse(
'2019-01-01 11:11:11'
)
# works
Thread
.abort_on_exception =
true
# fails
Thread
.
new
do
Time
.zone.parse(
'2019-01-01 11:11:11'
)
end
# NoMethodError: undefined method `parse' for nil:NilClass
In order to fix that, you need to set the time zone in each of your newly created threads:
Time
.zone =
'UTC'
Time
.zone.parse(
'2019-01-01 11:11:11'
)
# works
Thread
.abort_on_exception =
true
# works
Thread
.
new
do
Time
.zone =
'UTC'
Time
.zone.parse(
'2019-01-01 11:11:11'
)
end
Cover photo by Alex The Shutter on Attribution-NonCommercial 2.0 Generic (CC BY-NC 2.0) license.
Recommend
-
9
Rails -- NoMethodError undefined method unpack1 for nil NilClass Jan 26, 2020 I recently started a new application a...
-
5
Using the HTMLControl Class in Adobe AIR to parse HTML as a data source Friday, November 9, 2007 One of the cool features of Adobe AIR (especially for Flash developers) is its ability to render full featured HTML within Flas...
-
16
.attr ('class') is undefined on jQuery () advertisements I've got a problem with my jquery function in a wordpress theme. I've used exactly th...
-
10
JQuery $ (element) undefined when executing the .click () method - returns the item at all other times advertisements Have a really strange is...
-
8
Nick Scialli • May 14, 2021 • 🚀 4 minute readIf you’re working with React class components, you may find yourself vexed by ‘this.state’ being undefined. The Issue You...
-
9
Rails 3 method not defined `+ 'for nil: NilClass advertisements i am making a sorting table in rails and getting an error. This is my index.ht...
-
10
C # & ldquo; Undefined method & rdquo; When adding a new method to a simulated interface advertisements I am curren...
-
4
Separate user class [Parse] advertisements I'm building an app that users have to register before they can view the content, I a...
-
15
NoMethodError - undefined method 'find_by' for []: ActiveRecord :: Relation advertisements I've been following Michael Heartl tu...
-
4
问题解决系列:NameError: name 'platform_system' is not defined 精选 原创 枫夜求索阁...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK