Thanks Jeremy. That works a treat, however if I’m using an array
populated by values from a text file I get:
undefined method `-’ for nil:NilClass (NoMethodError)
I’ve tried flattening the array but it didn’t make any difference (as
that’s caused issues in the past), but to it’s no use.
Using IRB I can see it’s putting in new line characters when it’s
stripping the line.
i.e. “arrrrrrgh!\n”
I presume this is what is producing the error, does anyone know a way
around this error?
Thanks Jeremy. That works a treat, however if I’m using an array
populated by values from a text file I get:
undefined method `-’ for nil:NilClass (NoMethodError)
Because it’s not finding the string, so the index is nil, and you can’t
subtract from nil.
I’ve tried flattening the array but it didn’t make any difference (as
that’s caused issues in the past),
That’s cargo-cult programming. You’ve got to understand your errors,
not take wild shots in the dark.
but to it’s no use.
Using IRB I can see it’s putting in new line characters when it’s
stripping the line.
i.e. “arrrrrrgh!\n”
I presume this is what is producing the error, does anyone know a way
around this error?
Well, think about it. You can’t find “string” because the array
contains “string\n”. That suggests that you want to remove the \n from
the array. How do you do that? (Hint: it’s a one-liner.)