No Implicit Conversion from Nil to Integer - When Trying to Add Anything to Array

no implicit conversion from nil to integer - when trying to add anything to array

Implicit Conversion Errors Explained

I'm not sure precisely why your code is getting this error but I can tell you exactly what the error means, and perhaps that will help.

There are two kinds of conversions in Ruby: explicit and implicit.

Explicit conversions use the short name, like #to_s or #to_i. These are commonly defined in the core, and they are called all the time. They are for objects that are not strings or not integers, but can be converted for debugging or database translation or string interpolation or whatever.

Implicit conversions use the long name, like #to_str or #to_int. This kind of conversion is for objects that are very much like strings or integers and merely need to know when to assume the form of their alter egos. These conversions are never or almost never defined in the core. (Hal Fulton's The Ruby Way identifies Pathname as one of the classes that finds a reason to define #to_str.)

It's quite difficult to get your error, even NilClass defines explicit (short name) converters:

nil.to_i
=> 0

">>#{nil}<<" # this demonstrates nil.to_s
=> ">><<"

You can trigger it like so:

Array.new nil
TypeError: no implicit conversion from nil to integer

Therefore, your error is coming from the C code inside the Ruby interpreter. A core class, implemented in C, is being handed a nil when it expects an Integer. It may have a #to_i but it doesn't have a #to_int and so the result is the TypeError.

Type Error - No implicit conversion from nil to integer - Creating a array

The line nameArray.each do |x| iterates over the array and x is set to the value of the array at each index.

A better way might be to build the array using a map method. Something like this:

def array_maker 
puts "How many people would you like to enter? : "
num = gets.chomp.to_i
puts "\nEnter the names of the people you wish to add: "

nameArray = num.times.map do
gets.chomp.to_s
end

nameArray.each do |x|
puts x
end
end

array_maker()

Can't see why I have no implicit conversion from nil to integer error

Your index i is nil, as you are only doing map which iterates over the element only, to use map with index you need to use .map.with_index as shown in the docs which will give you both element x and index i

def block_word?(word)
word = word.upcase
@pair1.map.with_index { |x, i| word.include?(x) && word.include?(@pair2[i]) }.include?(true)
end

no implicit conversion from nil to integer

See http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-delete_at for the proper way of using Array#delete_at.

What you do is calling #index with a number that your array does not contain. Therefore $list.index(del) will return nil and the call to #delete_at will fail.

What you need to do is $list.delete_at(del).

No implicit conversion of Integer into Array

Working example

you need to re-read the question ... you have to replace the nums, and return the count of the remaining figures:

def remove_duplicates(nums)
nums.uniq!
nums.count
end

you have been returning the array, not the count of the array.

Adjustments to original code

to do it with your original code (note that the array should already be sorted, according to the question):

def remove_duplicates(nums)
nums.each_with_index do |num,i|
if nums[i + 1].eql? num
nums.delete_at(i)
end
end

nums.count
end

Working example with Unsorted initial array

if you wanted an answer that worked when the code is not sorted (which is not the case with your particular question, but a commenter seems to ask about this), and you need to generate a sorted unique array, overwriting the original array, and returning the size of the new array, you would need to do something like the following:

def remove_duplicates(nums)
nums.replace(nums.uniq.sort)
nums.count
end

Number in array not typed as integer?

You're getting into trouble here primarily because you're using for, and you're using it incorrectly. As odd as it might seem, for is hardly ever used in Ruby. Instead each and other Enumerable variants are encouraged.

Here's a more Ruby idiomatic version of your code:

def test(arr)
cache = [ ]

arr.each_with_index do |e, i|
cache[i] = true
end

cache
end

You can even refactor this further if you're doing a simple transform where the index isn't relevant:

def test(arr)
arr.map do |e|
true
end
end

If you inspected what value i was assuming during each iteration of your loop you'd see it creeps past the end of your array, and so ends up returning nil from an out-of-bounds call. .to_i converts this back to 0 which paves over the problem but doesn't fix it.

If you want to use ranges like that use 0...arr.length where the ... operator goes up to but does not include the ending bound.

Extracting recurring elements from array (Bad Value for Range Error)

There are a couple of issues here. From your example array it looks like the ending element is ':/desc' rather than ':/desc:' (i.e. no trailing :). That could just be a typo in the question though.

The main issue is that after removing the 2 slices the array will not be empty (it will still contain the "hello" from before the first start_element. This means that the array.any? condition will still be true when find_index(start_element) isn't going to find a matching element. In this case find_index will return nil, leading to a no implicit conversion from nil to integer when trying to use slice!.

If you know that your data will always contain start_element and end_element in matched pairs then one approach would be:

while start_index = array.find_index(start_element)
end_index = array.find_index(end_element)
final_array << array.slice!(start_index..end_index)
end

When faced with this kind of error in the future, some trusty puts debugging will help, in this case checking the 2 indexes and the remaining contents of the array:

while array.any?
start_index = array.find_index(start_element)
end_index = array.find_index(end_element)
puts "#{start_index}..#{end_index}"
final_array << array.slice!(start_index..end_index)
puts array.inspect
end

1..5
["hello", ":desc:", "claire", "caca", "concise", "test", ":/desc"]
1..6
["hello"]
..
TypeError: no implicit conversion from nil to integer
from (pry):146:in `slice!'


Related Topics



Leave a reply



Submit