11 Replies - 4123 Views - Last Post: 05 May 2018 - 01:31 PM Rate Topic: -----

#1 Dragoncreator   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 18

Traceback(most recent call lost)

Posted 04 May 2018 - 12:40 PM

I've created the following code:

    puts "Enter a filename:"
    afile = File.new(FileUtils.touch(gets.chomp).to_s, "w")
    afile.close
    #rest of code...



The file gets created, but after that the program ends with the error(see title) and I don't know what's wrong.
Can you give me a solution?

And thanks for help :D/>

Is This A Good Question/Topic? 0
  • +

Replies To: Traceback(most recent call lost)

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15222
  • View blog
  • Posts: 60,931
  • Joined: 12-June 08

Re: Traceback(most recent call lost)

Posted 04 May 2018 - 12:43 PM

Is that the entire error message? If not please copy/paste it here.
Was This Post Helpful? 0
  • +
  • -

#3 Dragoncreator   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 18

Re: Traceback(most recent call lost)

Posted 04 May 2018 - 01:02 PM

View Postmodi123_1, on 04 May 2018 - 12:43 PM, said:

Is that the entire error message? If not please copy/paste it here.


Yes. That's the full error message: Traceback(most recent call lost)

File creation with name works. But after that the program ends when you add something after it.
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 979
  • View blog
  • Posts: 3,858
  • Joined: 13-June 14

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 12:31 AM

View PostDragoncreator, on 04 May 2018 - 09:02 PM, said:

Yes. That's the full error message: Traceback(most recent call lost)


It isn't the whole error message. A traceback shows you the whole series of function calls up to and including where the error occurred (hence "most recent call last"). You shouldn't paraphrase error messages, because they contain important information about the problem. Please copy and paste them instead.
Was This Post Helpful? 0
  • +
  • -

#5 Dragoncreator   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 18

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 02:09 AM

View Postndc85430, on 05 May 2018 - 12:31 AM, said:

View PostDragoncreator, on 04 May 2018 - 09:02 PM, said:

Yes. That's the full error message: Traceback(most recent call lost)


It isn't the whole error message. A traceback shows you the whole series of function calls up to and including where the error occurred (hence "most recent call last"). You shouldn't paraphrase error messages, because they contain important information about the problem. Please copy and paste them instead.


The problem is that it doesn't show more because the program ends and instantly closes. Is there any way to keep it open?
Was This Post Helpful? 0
  • +
  • -

#6 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1393
  • View blog
  • Posts: 4,225
  • Joined: 12-May 09

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 10:27 AM

How are you running this program?

If you did something like:
ruby my-program.rb

from a command-line, the error should remain in the terminal.
Was This Post Helpful? 0
  • +
  • -

#7 Dragoncreator   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 18

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 11:50 AM

I experimented a bit with the problem and got the full error message(s) by using the interactive ruby.
That's it:

Traceback (most recent call last):
4: from F:/programs/Ruby25-x64/bin/irb.cmd:19:in `<main>'
3: from (irb):3
2: from (irb):3:in `new'
1: from (irb):3:in `initialize'
Errno::EINVAL (Invalid argument @ rb_sysopen - ["myfile"])

And thanks for the replies :D
Was This Post Helpful? 0
  • +
  • -

#8 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1393
  • View blog
  • Posts: 4,225
  • Joined: 12-May 09

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 12:00 PM

That error message could be better. It's saying that the value ["myfile"], which is an array with a single string, is not a valid argument to `File.new`.

Why are you `touch`-ing the file first? You should just be able to use new:
https://ruby-doc.org...ml#method-c-new

Something like:

aFile = File.new(gets.chomp, 'w+')
Was This Post Helpful? 2
  • +
  • -

#9 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1393
  • View blog
  • Posts: 4,225
  • Joined: 12-May 09

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 12:06 PM

An addendum for when you get this working - in Ruby (and many other modern scripting languages), it ends up being more idiomatic to let the language automatically close the thing you've opened when you're done with it, by accepting a block. For example:

File.open('stuff.txt') do |f|
  # call methods on f
end # here, ruby will close the file automatically.


Was This Post Helpful? 1
  • +
  • -

#10 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 12:09 PM

The problem is that touch returns an array of its arguments (more or less by accident, it looks like, because the last thing it does it to call .each on its argument array), but File.new doesn't want an array, it wants a string. It seems that you tried to fix that by calling to_s on the array, but that only makes the types work - the contents of the string aren't what you want.

The array you get back from touch is ["myfile"]. Now you call to_s on that and get back "[\"touch\"]". That is not a valid file name. Then you pass this invalid filename to File.new and get an error.

I should also point out that the fact that touch even has a return value is undocumented and should not be relied upon.

In conclusion I recommend that you do not use the return value of touch and instead just keep the original file name around:

puts "Enter a filename:"
filename = gets.chomp
FileUtils.touch(filename)
afile = File.new(filename, "w")
afile.close



That being said, calling touch at all doesn't make much sense here. Opening a file for writing creates it anyway, so touching it first makes no difference. So you can just skip the call to touch.

As another piece of advice, you should prefer opening files using File.open with a block, which will close the file automatically.

So this is how I'd write the code:

puts "Enter a filename:"
filename = gets.chomp
File.open(filename, "w") do |afile|
  # Code that uses afile goes here
end


Was This Post Helpful? 2
  • +
  • -

#11 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1393
  • View blog
  • Posts: 4,225
  • Joined: 12-May 09

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 12:11 PM

It's always nice to get confirmation of one's advice via a race condition. Well said!
Was This Post Helpful? 0
  • +
  • -

#12 Dragoncreator   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-May 18

Re: Traceback(most recent call lost)

Posted 05 May 2018 - 01:31 PM

View Postxclite, on 05 May 2018 - 12:00 PM, said:

That error message could be better. It's saying that the value ["myfile"], which is an array with a single string, is not a valid argument to `File.new`.

Why are you `touch`-ing the file first? You should just be able to use new:
https://ruby-doc.org...ml#method-c-new

Something like:

aFile = File.new(gets.chomp, 'w+')


It works! Thank you so much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1