Search code examples
rubyhighline

Ruby script does not work with weird errors about highline library


I have the following Ruby script:

begin 
      puts "What is the password? "
      the_pass = ask("") { |q| q.echo = "*" }
end while the_pass == nil || the_pass == "\n" || the_pass == ""

And it fails when I hit Enter:

undefined method default_external' for REXML::Encoding:Module /Library/Ruby/Gems/1.8/gems/highline-1.6.19/lib/highline.rb:621:in say' /Library/Ruby/Gems/1.8/gems/highline-1.6.19/lib/highline.rb:914:in get_response' /Library/Ruby/Gems/1.8/gems/highline-1.6.19/lib/highline.rb:259:in ask'

Looks like it fails when validating the input for the_pass, but I cannot understand the error, how are they related?

Thanks


Solution

  • This is bad error handling in the HighLine gem for Ruby < 1.9.

    The offending line (identified by your error message) is:

    statement.force_encoding(Encoding.default_external) if defined?(Encoding) && Encoding.default_external
    

    You can handle this by either:

    1. Removing any include REXML commands in your script. This will keep REXML::Encoding from being associated with Encoding.

    2. Adding the following line somewhere early in your script:

      REXML::Encoding.instance_eval { def default_external; false; end }

      This line will prevent the missing method error and will prevent HighLine from trying to force encoding where it shouldn't.