Question

I have a simple method

def save_logline
  print "What's the name of the movie"
  movie_name = gets.strip
  print "And what is your precious logline?"
  logline = gets.strip
  File::open(movie_name + '.txt', 'w') do |f|
    f.write(logline)
  end
end

Anytime I run the code, I receive the first prompt where I insert the name of the movie, but once I press enter the second print is called, giving me this message.

And what is your precious logline?=> 0

The file is written but I'm prevented from inserting anything into the text file. What's wrong with my logic and how do I fix this? I am using irb in RubyMine with Ubuntu.

EDIT: It appears that due to other users response to my code working and my own test on a different development machine my code is just not working on that one machine. The only thing I could say is that I was using RubyMine's irb console. The question now must be is it a RubyMine issue or is there a bigger problem with my dev machine?

FINAL EDIT: I checked on my second comp that has RubyMine, it appears that it does not work in that RubyMine's irb console either. I'm going to issue the bug to RubyMine and give credit to Jeremy in a day or two unless someone knows why RubyMine's irb is acting funky.

Was it helpful?

Solution

Your code works. The 0 is the return value of f.write. It's saying it wrote zero bytes to your file, because you didn't give it any input.

Edit: Maybe you pressed enter twice by accident...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top