TypeError
RubyERRORNotableType
Operation applied to wrong type
Quick Answer
Verify input types before operations; use .is_a? or explicit conversion methods like to_i, to_s.
What this means
Raised when an object is not of the expected type for an operation. Common in numeric coercion, type conversion failures, and operations that require a specific class.
Why it happens
- 1Adding incompatible types (e.g., String + Integer)
- 2Passing a non-hash where a hash is required
- 3Conversion methods receiving an object that cannot be coerced
Fix
Explicit type conversion
Explicit type conversion
value = params[:count] # likely a String from HTTP count = value.to_i # safe coercion; returns 0 on failure total = count + 10
Why this works
to_i, to_f, to_s perform best-effort conversions without raising TypeError.
Code examples
Reproducing the errorruby
"hello" + 42 # TypeError: no implicit conversion of Integer into String
Type guardruby
def double(n)
raise TypeError, "Expected Numeric, got #{n.class}" unless n.is_a?(Numeric)
n * 2
endRescue TypeErrorruby
begin result = value.to_str # strict conversion rescue TypeError => e result = value.to_s # fallback end
Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev