1

My ruby script doesn't seem to wait for my input. I'm trying to follow a script that encrypts your file from the first chapter of the book: wicked cool ruby, but it doesn't wait for my input after I run the file in command prompt.

Code snippet that is causing trouble:

require 'crypt/blowfish'

unless ARGV[0]
   puts "Usage: ruby encrypt.rb <filename.ext>"
   puts "Example: ruby encrypt.rb secret.stuff"
   exit
end

#take in the file name to encrypt as an argument
filename = ARGV[0].chomp
puts filename

c = "Encrypted_#{filename}"

if File.exists?(c)
   puts "File already exists."
   exit
end

print 'Enter your encryption key (1-56 bytes): '
kee = gets.chomp

begin
   blowfish = Crypt::Blowfish.new(kee)
   blowfish.encrypt_file(filename.to_str, c)
   puts 'Encryption SUCCESS!'
rescue Exception => e
   puts "An error occurred during encryption: \n #{e}"
end

What I see in cmd after I try to run the file:

C:\Users\me\>ruby new4.rb fileToEnrypt.txt
Enter your encryption key (1-56 bytes): An error occurred during encryption:
 can't convert String into Integer
C:\Users\me\>
4
  • 1
    It'll be great if you can provide more code. Commented Feb 7, 2013 at 14:09
  • Seems to work here, I don't see what the problem is.. Commented Feb 7, 2013 at 14:16
  • Just provide all of the code. Commented Feb 7, 2013 at 14:19
  • It's not necessary to chomp this: filename = ARGV[0].chomp. ARGV elements don't have trailing new-lines... well, they could but you'd have to go out of your way to put them there. Commented Feb 7, 2013 at 15:09

2 Answers 2

7

You have two problems here:

Concerning the fact that you don't get the user prompt, replace your code with:

kee = $stdin.gets.chomp

Concerning the error message, it's because:

The key is anywhere between 64 and 448 bits (8 and 56 bytes), and should be passed as a packed string

Sign up to request clarification or add additional context in comments.

3 Comments

This let me enter what I needed but I think Saurabh is correct in saying that gem is incompatible with 1.9.
Can you say why I need the $stdin.gets.chomp vs. merely the gets.chomp?
It seems that gets alone take the passed argument to script if any. If no argument, then it ask the user, whereas $stdin.gets always prompt the user.
0

This is because you are trying to convert the string into an integer by using encryption.

kee = gets.chomp is returning a string according to the input you are giving, which is a string, and, when you encrypt a string into integer, the error will occur.

The error message itself says:

can't convert String into Integer

Also, the gem you are using apparently has not been updated to work with Ruby 1.9.

The source says that there may be other incompatibilities in the gem.

3 Comments

According to the question, the script isn't even allowing anything to be entered. Notice the error message. There is no new line after the print message and the puts in the rescue block. This means the user is not even getting the chance to enter anything.
this is incompatible with ruby 1.9.
Thanks ByScripts actually answered my question, but you provide good information, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.