Ruby nonce throwing error
require 'date'
nonce = DateTime.now.to_i
Error:
undefined method `to_i' for #<DateTime:0x000000015336e8> (NoMethodError)
Working in my console it gives correct value
2.1.0 :014 > nonce = DateTime.now.to_i
=> 1405065242
Why it throws error programatically?
EDIT
Is there any way to add nonce. The condition is it should be a integer that has to be incremented on every subsequent request
As Pavan sais, if you run your code in irb it probably will not working anymore.
According to the doc, Ruby hasn't method to_i in DateTime class.
However, Rails override DateTime class to have a to_i method:
So, I think you run the command whick works in an Ruby On Rails environment, that's why it works. But if you run in a Ruby environment without Rails, it will not work.
Hope it helps.
Related
I'm currently unable to use any of the active support time methods inside my ruby on rails 5.0 model like the following:
5.seconds
2.days
10.minutes
throws an error:
NoMethodError: undefined method `seconds' for AS::Duration:0x007f97a5903b90 #value=5, #parts=[[:seconds, 5]] Did you mean? send
EDIT: here is the actual code causing an issue.
ReminderJob.set(wait: 5.seconds).perform_later(self.user.id)
Even tho I can see people using the below code fine and it works
UserReminderJob.set(wait: 1.week).perform_later user
However, it works in my console and in my controllers and views.
The error message states that the object is AS::Duration:0x007f97a5903b90, NOT an integer -- therefore the example of 5.seconds will not reproduce the problem.
This is also unusual, since 5.seconds will normally return an ActiveSupport::Duration object, not AS::Duration.
I would therefore hazard a guess that you're actually using the as-duration ruby gem rather than built-in rails behaviour. This extends the the Integer class in a different way, and returns an object that doesn't behave like an integer.
I think that an actual reproduction of your error could be achieved with: 5.seconds.seconds. In standard rails, this works fine (and returns the same value as 5.seconds), since ActiveSupport::Duration instances behave like Integers. But with this gem, it fails with the above error.
I am trying to read an environment variable in my worker(also tried in rake tasks) like:
puts ENV('APILAYER_KEY')
but it throws the following error:
NoMethodError: undefined method `ENV' for #<ExchangeRateUpdater:0x0055c5a7a484f0>
As I can see ENV is supposed to be Ruby method and will be available everywhere. https://ruby-doc.org/core-2.2.0/ENV.html
Has anyone seen this problem?
Change the braces (as it's shown in the docs you've linked to):
ENV['APILAYER_KEY']
ENV is an object. The problem with your code is that Ruby treats it as if it was a method, and "APILAYER_KEY" as an argument passed to it.
So i found this strange anomaly while working in a gem that we are using internally.
We have this private method
private
def redirect_to_element(element, next_upload)
send("scorecard_#{element.base_class_name.underscore}_path", current_scorecard, current_tab(element, next_upload))
end
Which just builds a path dynamically depending on what element is passed to it. What i would like to do is have those dynamic paths pass some params. But i get this error
undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>
so in the console i tried several things and this is what i found.
>> scorecard_enterprise_development_path
=> "/scorecards/338/enterprise_development"
>> send('scorecard_enterprise_development_path')
=> "/scorecards/338/enterprise_development"
>> scorecard_enterprise_development_path(ignore_tracking: true)
=> "/scorecards/338/enterprise_development?ignore_tracking=true"
>> send('scorecard_enterprise_development_path(ignore_tracking: true)')
!! #<NoMethodError: undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>>
That using the send method to build a path with params will fail. Can anyone explain why this happens?
I am using, ruby -v 1.9.3p327 and rails -v 3.2.16
#send will invoke the method identified by the first argument and pass it any arguments specified.
So you should use the method this way:
send('scorecard_enterprise_development_path', ignore_tracking: true)
See the send documentation
I am using rails 3.2.16 and ruby 1.9.3
I get this error from the localhost browser after rails s.
undefined method `to_date' for nil:NilClass
like this
NoMethodError in Products#new
Showing /Users/main/railscasts-episodes/episode-350/store-
after/app/views/products/_form.html.erb where line #27 raised:
undefined method `to_date' for nil:NilClass
in irb> mode it works only when I
require 'date'
Now my question is about my rails project. In which file in my project I should add
require 'date'
I added it into my model it did not work. Or, maybe I need to install any specific gem? to make this work
The problem you have isn't an issue with to_date, it's a problem with the object you're trying to perform the method on
Your issue will either be:
You have not declared the variable / object
Your variable / object is not populated with any data
I'd highly recommend posting the controller action you're using to invoke the to_date method. Obviously, there may be an issue with the way you're calling it, but the immediate issue is you're trying to deal with an unpopulated variable
I am noticing differences between a hash object within Ruby 1.8.7 and a hash object within Rails 3.0.10.
For example, within the 1.8.7 irb, I get:
1.8.7 :001 > {}.try(:method)
NoMethodError: undefned method `try' for {}:Hash
from (irb):1```
However, from the 3.0.10 rails console, I get:
1.8.7 :003 > {}.try(:method_x)
NoMethodError: undefined method `method_x' for {}:Hash
from (irb):3:in `try'
from (irb):3
This surprises me because I was under the impression that try is defined in Object which is an ancestor of Hash and try will return nil instead of throwing a NoMethodError.
What am I missing?
This surprises me because I was under the impression that try is defined in Object which is an ancestor of Hash and try will return nil instead of throwing a NoMethodError.
What am I missing?
Your impression of which class try is defined in is correct (Object). What you are missing is what file it is defined in. It's defined in the ActiveSupport library, not in the Ruby core library.
So, you need to
require 'active_support/core_ext/object/try'
first.
try is not part of ruby 1.8.7, though Rails does include it through ActiveSupport. try is part of Object from ruby 1.9+ (afaik).