TypeError: no implicit conversion in CI
Ruby refused to auto-convert one type into another for an operation. A string concatenation, array index, or similar got a value of an unexpected type.
What this error means
A test fails with TypeError naming the two types involved, such as "no implicit conversion of nil into String". The value feeding the operation is not what the code assumed.
TypeError:
no implicit conversion of nil into String
# ./app/helpers/path_helper.rb:5:in `+'Common causes
nil where a String was expected
A value that was nil (an unset ENV, a missing key) was concatenated or interpolated into a String operation that does not accept nil.
Wrong type from a method
A method returned an Integer, Symbol, or Array where downstream code expected a String, and the operation will not coerce it implicitly.
Mixed types in an operation
Array#[] or similar received a non-Integer index, or arithmetic mixed incompatible types.
How to fix it
Convert explicitly or fix the source
- Identify the value of the wrong type from the backtrace.
- Either fix where it gets the wrong type, or convert explicitly with to_s, to_i, or to_a as appropriate.
- Guard against nil with fetch or a default before the operation.
Coerce at the boundary
Normalize values to the expected type where they enter the code.
path = "/" + segment.to_s # nil becomes "", no TypeError
index = key.to_iHow to prevent it
- Convert external values to the expected type at the boundary.
- Use fetch with defaults so nil never reaches type-sensitive operations.
- Add a test for the empty/nil input path.