views:

110

answers:

1

How does one remove trailing character correctly in the following sentence if it's in config/environment.rb file.

KEY = ENV['KEY'].delete "\r"

It produces the following error:

undefined method `delete' for nil:NilClass (NoMethodError)

It works well in IRB, but not in environment.rb

Solved

Aptana Studio 3 stopped to load .bashrc after the latest update. Thanks to Mladen and Mark for help.

+2  A: 

String#chop returns a copy of the string with the last character removed. And it has a counterpart String#chop! which mutates the string as well.

However, your particular error (undefined method 'delete' for nil:NilClass) means that ENV['KEY'] returned nil, which of course does not respond to the delete message. You could try

KEY = ENV['KEY'].to_s.delete "\r"

to coerce it to a string. nil.to_s returns the empty string, and "".delete x will still be "". On the other hand if ENV['KEY'] does correctly return a string, nothing different will happen than if you didn't include to_s.

Mark Rushakoff
Mark, thanks. I have updated my question - the problem is only in `environment.rb`
Andrey
There's also `String#chomp`, which removes only newline chars.
Mladen Jablanović
It wasn't clear (to me) if he wanted to remove `"\r"` and keep `"\n"`. `String#chomp` will of course remove both.
Mark Rushakoff