From: Marnen Laibow-Koser Date: 2009-11-24T23:57:26+09:00 Subject: Re: Best way to search through an array for an item matching Daniel Webb wrote: > 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.) > > > Thanks very much, > Dan Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.