1

I am writing a class, Wrapper, and am giving it a class method self.wrap. I am getting an unexpected end-of-input error. I have been staring at the code forever and can't see where the mistake might be. I think I have all of my end's in place. What am I missing?

require 'pry'
class Wrapper
    def self.wrap(input_string, column_number)
        position = 0
        num_inserted = 0
        while (position + column_number < line.length) do
            last_space_index = input_string.rindex(" ", position + column_number)
            if last_space_index > position - 1
                input_string[num_inserted + last_space_index] = "\n"
            else
                input_string.insert(num_inserted + position + column_number, "\n")
                position += column_number
                num_inserted ++
            end
        end
    end
end
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

1

There is no ++ operator in Ruby.

Instead, you can write:

num_inserted = num_inserted + 1

Or for short:

num_inserted += 1

The error message you're seeing is admittedly quite confusing/misleading.

However, if you use an IDE with language-aware syntax highlighting (something I would highly recommend all programmers, in all languages, set up as a high priority for development) then this should at least highlight exactly which line of code is causing the problem.

For example, entering your code in my local setup reveals the error:

`+' after local variable or literal is interpreted as binary operator
even though it seems like unary operator
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Can you recommend an IDE? – Peter Mortensen Jan 13 '21 at 17:45
  • 1
    There are plenty of articles discussion your options in depth, such as [this](https://www.rubyguides.com/2019/02/ruby-ide/). Personally I like `Vim` for its speed and cross-device reliability, or `VSCode` for a more enhanced GUI. At the end of the day it doesn't really matter what you use, but you're developing in handcuffs if your editor doesn't automatically flag syntax errors like the above for you. – Tom Lord Jan 13 '21 at 19:36