No implicit conversion of String into Integer (TypeError)?
Write as
@systemid = @getsystemid[0]['id']
Your @getsystemid
is not a Hash
, it is an Array
of Hash
. @getsystemid[0]
will give you the intended hash {"id"=>1000010466, "name"=>"cfme038", "last_checkin"=>#}
. Now you can use Hash#[]
method to access the value of the hash by using its keys.
No implicit conversion of string into integer on ruby on rails
The root of the problem is that x
isn't a hash. I expect it's an array.
For example this will give you the same error:
[]["symbol"]
Basically, when x
is an array, the input of the []
method expects an integer, and therefore tries to convert what is passed in, into an integer. The error occurs when the conversion fails.
Why do I get no implicit conversion of String into Integer (TypeError)?
From the exception, it seems that json
in the second response is an array with only one element, so the output of puts json
is the same (brackets don't get output with puts
), but json["string"]
fails because []
expects an Integer
to use as index.
Check p json
or that json.is_a?(Array)
and if it is indeed an array, try with json.first['imdb_id']
.
TypeError: no implicit conversion of String into Integer in ruby
Looks like you are trying to print the ids of the objects which are in a Array.
Try this: puts "{billing_ids.map(&:id)}"
About the error TypeError: no implicit conversion of String into Integer
The error is because you are tring to access an index "id" of an Array
of Billing objects. The id
attribute of a particular object can be obtained but if you try accessing id
of the Array, thats not going to work. (Note billing_ids
is an Array as per your code). Array can be accessed by indices which are integers (unlike Hash
)
ruby - json no implicit conversion of String into Integer (TypeError)
The exception:
[]': no implicit conversion of String into Integer (TypeError)
says that @current
is Array
, not Hash
, and since index to an array can be the only number, you get the exception. You can see it by printing the inspected value with:
puts @current.inspect
So solution is to use [0]
, or #first
method, in the assignment:
@current = @parse['data']['current_condition'].first
Opening file with write throws No implicit conversion of String into Integer
As per the docs, the second argument for File.read
is the length of bytes to be read from the given file which is meant to be an integer.
Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning.
So, in your case the error happens because you're passing an argument which must be an integer. It doesn't state this per-se in the docs for File.read
, but it does it for File#read
:
Reads length bytes from the I/O stream.
length must be a non-negative integer or nil.
If you want to specify the mode, you can use the mode
option for that:
File.read("filename", mode: "r") # "r" or any other
# or
File.new("filename", mode: "r").read(1)
Why am I getting no implicit conversion of String into Integer when trying to get Nested JSON attribute?
because "provider" is an array.
it should be accessed with index.
[:value][0][:provider][0][:name]
same goes with "value".
Related Topics
Respond_To' VS. 'Respond_To_Missing'
Ruby on Rails, Could Not Find a Valid Gem 'Rails'
Openssl, Rvm, Brew, Conflicting Error
Resque Multiple Workers in Development Mode
How to Specify Post Params in a Rails Test
Sidekiq List All Jobs [Queued + Running]
How to Get Searchlogic to Work with Rails 3
How to Use One Line Regular Expression to Get Matched Content
Rails Plugin for API Key + Secret Key Signing
How to Install Gems with Apt-Get on Ubuntu
How to Check from Ruby Whether a Process with a Certain Pid Is Running
Which Ruby Gems Support the Facebook API
How to Structure a Layout Template in Haml
Cucumber Not Showing Coloured Output in Windows