Ho un problema (sono uno nuovo al linguaggio che lo sta imparando)

#1

Se scrivo
file.open (“Leggimi” “r+”)
mi da “traceback” e il programma (fatto con Command Prompt) non funziona.
Qualcuno mi sa dire perchè?
Ho messo la virgola e mi diceva errore, tra l’altro

#2

Hi, I am so sorry for not understanding Italian. But I may help you in English. And I am not going to bother you with an incorrect Google translation! So sorry for that :frowning_face:

Anyways, Here’s the Correct Version of your Code :lollipop:

File.open(“Leggimi”, “r+”)

Description :spiral_notepad:

1. File is a Class in Ruby:

File.class    # => Class

2. open is a method of File class:

File.respond_to?(:open)         # => true
# or
File.methods.include?(:open)    # => true

3. () and the , :

Because it’s Ruby you have a tonne of alternatives!

But the parentheses should be right after the File.open. If not it’s evaluated first and it really makes no sense to Ruby, and it raises SyntaxError !!
You can even omit the () but you have to use the ,.
Nevertheless, you can call a method in many ways:

File.open('Leggimi', 'r+')    # => #<File:Leggimi>
# Or
File.open 'Leggimi', 'r+'    # => #<File:Leggimi>
# Or
File.method(:open).call('Leggimi', 'r+')   # => #<File:Leggimi>
# Or
File.instance_method(:open).bind(File).('Leggimi', 'r+')    # => #<File:Leggimi>
Suggestion: The File.open is not required! You can use open instead:
open('Leggimi', 'r+')    # => #<File:Leggimi>
# Or even:
Kernel.open('Leggimi', 'r+')    # => #<File:Leggimi> # But I don't suggest using it!

Hope this helps!

#3

Thank you. I’m gonna see if this helps. Anyway I didn’t use a comma because the command prompt said it was an error.

#4

Yup, the error was putting the parentheses! I mean you have a space before the parentheses, and that doesn’t make it an argument to File#open. You can omit the parentheses too:

File.open 'Leggimi', 'r+'

But if you put parentheses, don’t leave any space before!

The previous answer is updated! Please take a look!

#5

That doesn’t work either. Perhaps if I cancel the last version of Ruby (2.6) that I installed with 2.5 to update it, I’ll resolve the problem, because I read somewhere that the last version is bugged. I’ll cancel both versions, that I’ll install 2.5. Sorry for my bad english.

#6

Ruby is not bugged in such simple stuffs. Bugs sometimes lie in Regexp and these stuffs (and I once heard about it, it’s fixed now). I don’t know what you are writing wrong. The program in the question makes no sense, you used space and parentheses, then omitted the comma, and it raised SyntaxError - that’s a normal behaviour.

Ruby is not a magical programming language and will not understand whatever you are trying to do. Ruby has syntax. Ruby is a huge programming language, calling this simple SyntaxErrors as Ruby bug is like saying a BMW is bad because you can’t drive cars.

Anyways, I discussed may ways in the above posts. I have tested the code on many older Rubies, JRuby. Even crystal language will work with File.open().

If you are using MRuby, then simply avoid everything I have told you.

Can you paste the code from the File.open to you are closing the file?

#7

Yes, I know that working with Ruby (as with other languages) is not just typing code and running it. But I tried to fix the bug for 2 weeks, and I tried every method on the Internet (and all the methods that worked for others didn’t work for me). Maybe if I cancel all the versions of Ruby and I install it again, it will work. The code that command prompt shows after running the program is only “Traceback”. How can I cancel Ruby from my computer (all the versions)?

#8

Can you paste in the code here?

I don’t know, if there’s any installation problem… Personally speaking, I never had the issue on any of my systems (all are Linux), so I am ignorant about installation issue.

If you are not using MRuby, can you open up your IRB and type:

puts RbConfig::CONFIG.map { |v| v.join(' => ') }

This will show all the details! Also, note down the Ruby version. Can you please comment back?

If there’s no issue running the above code, then I think your installation is fine! Also, note that if you are using Windows (though I used only virtualmachine and never used bare metal windows), you may have problems with opening the file. I suggest you always use this following code to avoid Errno::ENOENT error:

PATH = File.dirname(__FILE__)    # or $PROGRAM_NAME if you are not requiring it
File.open(File.join(PATH, 'Leggimi'), 'r+')

And this is really what I do, and I have seen many codes that use this.
Please confirm the above results then we might understand what’s the real problem!

#9

Scusa se ti ho risposto ora, sono stato molto impegnato questo periodo con gli esami, dato che andavo ancora a scuola. Comunque proverò e ti dirò i risultati. Grazie per l’aiuto.

#10

Sure! No worries. Let’s just follow my last answer!

#11

C’è un problema nel funzionamento del codice, infatti il command prompt dice che non lo riconosce come comando, ma i comandi come “$gem install” sono riconosciuti.

#12

Can you access IRB?
Just type:

irb

You already know, you will have a REPL like this:
image

If irb isn’t recognized, there could be an installation problem. Also, if your ruby is MRuby, all the comments I made might not work. If your Ruby is MRI, JRuby, and older MRI like 2.3.8, then it should work…