In my application I'm seeing the following exception:
TypeError: nil can't be coerced into Float
I'm iterating through a hash and running the following:
v[:score] = (1 / v[:distance].to_f) * v[:weight]
However, when I try to debug at the rails console, I don't get an exception. In fact, if I run:
nil.to_f
I get 0. So at the console running my line above yields Infinity since I'm dividing by zero.
So my question is, it seems that nil CAN be coerced to a float, so why am I getting an exception in my application?
Thanks!
The error can occur :
arup#linux-wzza:~> pry
[1] pry(main)> (1/2.0) * nil
TypeError: nil can't be coerced into Float
from (pry):1:in `*'
[2] pry(main)>
In your app I am sure v[:weight] is giving nil. Problem is not from v[:distance], as you have handled it v[:distance].to_f. Check twice, I am damn sure, v[:weight] is nil in any situation.
Related
for what reason dask astype givers error if errors keyword is specified, while that should work fine according to the docs?
df['listing_id'] = df['listing_id'].astype('int32', errors='ignore')
Here is the error :
TypeError: astype() got an unexpected keyword argument 'errors'
The doc also says
This docstring was copied from pandas.core.frame.DataFrame.astype.
Some inconsistencies with the Dask version may exist.
You were warned :)
As I issued here, I am trying to store/restore an object into/from the database, I have the following snap:
kmeans = KMeansClusterer.run(k, colours, runs: AppConfig.fabric.colours.cluster.runs)
kmeans.predict [[1, 2, 3]] # <- works fine
(YAML::load(YAML::dump(kmeans))).predict [[1, 2, 3]] # <- ERROR
When I am trying to dump/load the object with YAML, I get the following error
ERROR: *** TypeError Exception: wrong argument type NMatrix (expected Data)
Question: How can I store/restore the kmeans object without any trouble?
Serialization needs cooperation of the objects.
NMatrix does not appear to support serialization.
I am able to launch new instance at AWS from Ruby on Rails application (Chef::Knife::Ec2ServerCreate.new()). Works fine till I try to set JSON attributes. When I set them from command line and invoke knife.bat, it works.
Looking at ec2_server_create shows that command line option --json-attributes is mapped to symbol :json_attributes. I tried to set it using following code:
Chef::Config[:knife][:json_attributes] = "{'NodeName' : 'Node001'}"
and I get error:TypeError (no implicit conversion of Symbol into Integer):
As soon as I comment this single line, instance is created, registered with chef server and recipe is running.
Any suggestion how to set json attributes of first chef-client run?
PS
Sample code shows constant value for attribute, but actual value would be dynamically created.
Error message and code line where error occurs:
/chef/knife/core/bootstrap_context.rb:188:in `[]=': no implicit conversion of Symbol into Integer (TypeError)
Looking into source you can find:
def first_boot
(#config[:first_boot_attributes] || {}).tap do |attributes|
if #config[:policy_name] && #config[:policy_group]
attributes[:policy_name] = #config[:policy_name]
attributes[:policy_group] = #config[:policy_group]
else
attributes[:run_list] = #run_list #THIS LINE CAUSES EXCEPTION
end
attributes.merge!(:tags => #config[:tags]) if #config[:tags] && !#config[:tags].empty?
end
end
I set run list.
The issue is that json_attributes needs to be a hash, not a string. It isn't the #run_list that is failing, it is "{'NodeName' : 'Node001'}"[:run_list] which when you see in on place is probably a bit clearer.
ActionView::Template::Error (PG::UndefinedFunction: ERROR: operator
does not exist: double precision ~~ unknown
2016-04-10T23:45:59.506005+00:00 app[web.1]: LINE 1: ... =
"trackers"."category_id" WHERE (categories.tag LIKE '1.%'...
this is the error i get when i try to run this line of code here
Tracker.group(:category_id).joins(:category).where('categories.tag LIKE ? AND user_id = ?', "#{tag.to_i}.%", current_user.id)
tag is of type float, and i typecast it to an integer in order to check for tags 1.1, 1.2, 1.3 etc
so in the example above I type cast tag with value 1.0 to be 1, so i can search for tags that are like 1.1, 1.2 etc
I am using postgres on heroku that gives this error. locally i use sqlite3 and it works just fine.
how can i overcome this?
Since you're in rails, sort out the dynamic-ness in rails first then send that to the ORM. The syntax you provided already accepts any parameters (eg: WHERE tag between ? and ?), so before you request the data from the ORM, sort out in rails the high and lows. The query is already setup for something to be dynamic.
I have this strange error in one of my rails 2.3.2 application.
NoMethodError in Timesheet#index
undefined method '>=' for nil:NilClass
Extracted source (around line #27):
24: for alog in act_logs
25: if alog.user_id == session[:user_id].id
27: if(alog.log_date>=#dt.beginning_of_week()&& alog.log_date<=#dt.end_of_week())
As far as I can guess, I think either alog.log_date or #dt.beginning_of_week is nil. But a quick inspection just before line #27(inspection not included here) shows the following values:
#alog.log_date
Wed, 09 Feb 2011
##dt.beginning_of_week()
Mon, 10 Oct 2011
Now if none of these values are nil why do I get this undefined method >= for nil:NilClass error on line #27. If you guys are wondering why I call this strange....its because
1. If I use == operator, everything's ok. Other operators like >, < generate the same error.
2. This same code is working on another machine.
I don't think that it's a code problem here. Has it got something to do with the Ruby or may be Rails installation problem?
The error means precisely that alog.log_date is nil. alog.log_date>=#dt.beginning_of_week() is the same as alog.log_date.>=(#dt.beginning_of_week()), i.e. >= is just a method call (with some syntactic sugar). If the latter would be nil, you'd instead get something like this:
ArgumentError: comparison of Time with nil failed
You could add an existence check for the date in the beginning of the line:
if (alog.log_date && alog.log_date >= #dt.beginning_of_week() && alog.log_date<=#dt.end_of_week())
However, if you think that date should never be nil you might want to add a default value and/or a presence validation for the field.
Good technique to catch such a bug is to attach debugger
or simplier one is to write values to log, you should just put:
Rails.logger.info( variable_to_log )
before error line
PS
it works with equality operator '==' as nil object has this method
your comparison operator exists for integer class only