[Windows] Difference between using Command Prompt vs. Interactive Ruby Shell


#1

I have a very basic script that I want to run in an Interactive Ruby shell that I downloaded from https://rubyinstaller.org/. When opening Interactive Ruby Shell,

irb(main):001:0>

automatically appears. I’ve attempted numerous times to try and check the program for syntax errors, but cannot find a way. Normally, on the command line in Windows:

$ ruby -cw fileName.rb

is what I would have prompted in the command line to check for syntax errors. Is there any way to do this via the Interactive Ruby Shell? Does the Interactive Ruby Shell have the capabilities to check syntax?


#2

Cannot say for sure but one would expect the shell will catch syntax errors only when a line is entered, or when we attempt to run a script that contains syntax errors.


#3

Your question is not quite clear to me. Is it that you basically want to verify from within your irb shell, that a String contains syntactically valid Ruby code, without actually executing it, or do you want to know from within irb, that a file contains syntactically valid Ruby code?

In the latter case, you simply can do a

system('ruby -cw fileName.rb')

In the former case, I think you could use

RubyVM::InstructionSequence::compile('your string goes here')

but I have never tried it.

Of course, the question is why do you WANT to do this from within irb? It doesn’t really make sense to me.


#4

@mtf : Of course syntax error will be shown for each line you enter in irb, respectively when running a script (by, say, load ‘filename’), but this is not what the OP asked. He wants to syntax-check it without running.

Of course, a syntax check doesn’t reveal much, because it is easy to produce a Ruby program which is syntactically correct, but at least one gets some errors by syntaxchecking - missing closed parenthesis and so on. I do it often during development. I just never had any need to do it from within irb…


#5

Thanks for the response @uhudler! I am trying to familiarize myself with the Interactive Ruby Shell by comparing its capabilities to that of the versatility of the command line. I apologize if my questions seem elementary, but I spent a couple of hours searching for an answer and could not find one. Albeit I am very new to Ruby (and new to programming in general), I want to see if there are different ways to work around certain tasks. If you have any helpful links for Windows command line and Interactive Ruby Shell, it would be greatly appreciated!

Thanks for your time!


#6

Well, you didn’t say that you are on the Windows operating system.

I usually work with several console windows open. One (or more) running irb, where I can quickly try out some Ruby expression (this is why I don’t have any need to just syntax-check them - I want to run them!) and one or more windows running some shell, from where I’m launchin programs and so on. Technically you could do all of this from within irb, but to what extend you consider it convenient, you have to find out by yourself. I find for many tasks using a shell easier than doing it from Ruby.

Since I don’t like neither the “old” Windows “DOS-shell” nor the Power Shell, I’m using on Windows the Cygwin environment, and within it I use the Z-Shell (zsh) as a shell, and mintty (which is also a Cygwin program) as a console (which I also prefer over the Windows console), but here again, this is a personal choice. I also run irb inside a mintty console.

I understand that, if you are new to programming in general, this might sound a bit complicated. Basically, you need two things for a start: A good text editor, and a console (which could be the Windows command prompt console), from which you will start several instances (some of those consoles running irb, some just the command prompt of your shell).

Just one note for the practical side: Once you have installed the text editor of your choice, set it up so that it generates files with just LF line endings (the default on Windows is usually CR LF). This saves you a lot of trouble if you later want to run your Ruby programs on, say, Linux, or in a Cygwin environment.

Ronald


#7

Bottom line: Syntax checking is something the WE do, not the IDE. We depend upon it for exception checking which is the errors committed during the run of the program.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.