sixty4bit sixty4bit - 2 years ago 93
Ruby Question

"No such file" error when using `gets.chomp` in seeds.rb

I wanted my seeds.rb file to have two paths based on some user input. For the sake of simplicity in this question I've stripped it down to just these two lines:

print "> "
res = gets.chomp

When I run rake db:seed, the following exception is raised:

▶ rake db:seed
> rake aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - db:seed
/home/me/work/my_app/db/seeds.rb:5:in `gets'
/home/me/work/my_app/db/seeds.rb:5:in `gets'
/home/me/work/my_app/db/seeds.rb:5:in `<top (required)>'

Anyone know why this is happening, i.e. why gets.chomp in this context is causing the program to try to open a file named db:seed?

Answer Source

Try using STDIN.gets.chomp instead of gets.chomp.

See What's the difference between gets.chomp() vs. STDIN.gets.chomp()? for the explanation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download