Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Member-only story

Exploring Ruby errors — exercise 1.

--

I don’t know about you but errors make me cringe. They make it evident that I am not in control of my code and I don’t like it. This is the motivation behind this series of short articles where we explore errors together. Look at them, read what the error message says, follow the line to where it occurs and understand why it happened.

Take the code above, it will produce an error. Think if you know the answers to these points:

  1. Do you know which error will be thrown?
  2. Do you which line of the code will cause the error?
  3. Do you know why this error is thrown?

Here is the code if you want to copy it and run it (and if you don’t have ruby on your system, you can run the code in the browser here: https://runrb.io/:

def rotate_array(arr)
arr[1..-1] + arr[0]
end
p rotate_array([7, 3, 5, 2, 9, 1]) == [3, 5, 2, 9, 1, 7]

The answers:

  1. When we run the snippet above, Ruby throws this error: TypeError: no implicit conversion of Integer into Array from solution.rb:37:in `+’
  2. The error itself also provides us the line, on which error occurred. Line 37, which is the last line of the method.
  3. As to why, well, Ruby error explains it. It says that there is no implicit conversion of Integer into Array. The problematic line is:
arr[1..-1] + arr[0]

--

--

Ewa Barczykowska
Ewa Barczykowska

Written by Ewa Barczykowska

Hey, I'm Ewa(pronounced: Eh-vah), I'm a DevOps engineer at a large telecom company.