Rubyinstaller for Windows - ruby does nothing - ruby

I've tried using Ruby 2.0 x64 and Ruby 1.9.3 for Windows using RubyInstaller. Entering ruby -v works as expected, and running gem gives me the expected usage docs. Running and using the Interactive Ruby application works as expected. I am running Windows 8.1 Update.
However, for both installations, running ruby from cmd gives me a blank prompt where I can type, but nothing is executed when I press enter. If I attempt to install a gem, there is a similar issue where the program is running, but there is absolutely no output, and nothing happens.
I can't seem to be able to find a similar issue elsewhere. Does anyone know what might be wrong, and how I could fix it?

What did you expect to happen? ruby.exe is the ruby interpreter, meant for running ruby scripts. Normally, to use it you would create a file containing valid ruby commands with your favorite text editor (but not a word processor). If you save the file as foobar.rb, typing ruby foobar.rb (or if you told the installer to associate .rb files with ruby, typing just foobar.rb) will execute the commands in the file as a script/program. If you don't supply a script file name, ruby goes into input mode and expects you to type in a program on the spot. It won't give any feedback until you indicate end-of-file by typing CTRL-z, at which point it will process what you typed and most likely tell you about all the errors you made. If you want line-by-line interactive feedback, use irb.

Related

I can't run my Ruby program and I see errors in the terminal. I can't find the program

Good afternoon! I have a problem with Ruby. I downloaded this program from the official site https://www.ruby-lang.org/en/downloads/ on Fedora without any problems (using the command via the terminal). However, I cannot use the 'irb' command in the terminal to run Ruby on Linux as described here https://www.ruby-lang.org/en/documentation/quickstart/. Therefore, I cannot learn the Ruby programming language.Command 'irb' not found
I tried my best to run Ruby in other ways. One of them, as is customary in Fedora, is to write the name of the program in the terminal. But it opens a folder in which, I assume, Ruby is located (I can't find the folder itself either in Files or via the 'cd' command in the terminal). I kind of can open this folder in the terminal, but I can't find it in Files.
Oh, by the way, the 'irb' command doesn't work there. Only some real command for a program like this one will work: irb(main):001:0> "Hello World" (from https://www.ruby-lang.org/en/documentation/quickstart/). The program is and is not at the same time
Move on. I made a text document, wrote irb(main):001:0> "Hello World" in it (as written in "Ruby in Twenty Minutes"), and saved the file as hello.rb . Opened the hello.rb file path through the terminal and wrote 'ruby hello.rb' (without quotes, of course). Result: syntax error. hello.rb file Tried to open hello.rb via ruby in terminal
Oh yeah, I almost forgot: when I tried to open hello.rb in Files through another application, there was no Ruby script in the list (ruby or irb or at least something through which one could open a .rb file). No program for .rb format
I hope I have explained the problem in sufficient detail and clearly. And I hope for your help and understanding! In any case, good mood to you!
On Fedora & CentOS, the Ruby installation is split into many smaller packages. If you want to run irb, you'll also have to install the ruby-irb package using dnf install ruby-irb.

curl runs from command line, but not in ruby script

The following command (and variations of it) run okay in terminal in mac, run okay in a ruby program executed on a mac, run okay directly in the windows command prompt, but fail with a parsing error when I try to run it inside of a ruby file on windows.
curl -u"user:pwd" -d"{\"name\":\"new_repo_beepo\"}" https://api.github.com/user/repos --insecure
I've tried executing it with backticks, %x() and system. I've also tried substitution of strings and json'ing pieces of it, without any luck. From what I can determine, the failure point is in the -d"{\"name\":\"repo_name\"}" section, but that's only from trying the command without it. Regardless, in each variation of the command on windows in ruby, I get a JSON parsing error.
Have you tried using rest-client?
It is a gem and works quite nice. It is probably better than using system()or %x() and it is definitely more secure (You can inject malicious bash commands on system() so it needs to be used carefully).
It is quite simple to use. Just install the gem and require it one your ruby file.
NOTE: If using Rails just add it to your Gemfile(No need to require it on each file).
require 'rest_client'
RestClient.get 'https://api.github.com/user/repos', {params: {id: 50, foo: 'bar'}}
You can also use some params for --insecure ssl.
It seems likely this is a parsing/quoting problem with the shell mechanism used by ruby to run the command on windows. Have you tried the tokenized form of system, eg:
system('curl', '-u"user:pwd"', '-d"{"name":"new_repo_beepo"}"', 'https://api.github.com/user/repos', '--insecure')
On posix that will send the arg vector as specified directly to the exec'ed program without letting the shell get in the way; probably the same semantics hold for windows.
You shouldn't need to exec curl to do this as ruby has stdlib Net::HTTP. In comments you mentioned that you've had problems with this module under jruby, but we have jruby services here exercising http[s] requests just fine, so you might try posting a question addressing the specific problems you have with jruby and native http client libs.

How to disable stdin echo in windows console

I'm writting a Ruby program for windows, and I need to read user input from keyboard (stdin).
However, it's needed that the user key-presses are not automatically displayed in the windows console, and behave like "silent key presses"
This problem in Ruby over linux, can be solved using the "stty" linux command:
%x{stty -icanon -echo}
because it is the linux terminal who automatically outputs the user-keys into the terminal, so running the "stty" command tells the terminal to stop showing the user-key-presses.
But my program must run in windows, so I tried searching for a "stty" equivalent command for windows console, but still found nip...
?any suggestions, pointers ?
Look at Highline gem. To clarify, look at ask method and provide a block to silence it's output. It is well exemplified in their documentation

How to write ruby code easier?(I mean in terminal write and then run it)

When I write a little ruby code, after a little bit, I always need to create a new terminal tab to ruby it, to see if it's correct.
Are there any ways to do it in one window? Like a vim plugin or some other tool?
The following should work in vim, after you've saved the file:
:!ruby %
Or even
:!%
This works under Linux when you have the correct "shebang" as the first line of the ruby file:
#!/usr/bin/env ruby
For extra fun, you can map this to a key in your ~/.vimrc:
map <F8> :!ruby %<CR>
Do you mean you need an interpreter to see what your code does? If so, check out irb.
The way you should check if your code works is using unit tests, not running it in the console or irb. Indeed, irb is a good solution for small fragment of code or to check for specific statements.
However, there are some solutions to your specific question.
You can write the code in a file, save it and run it from the console.
ruby filename.rb
If you use TextMate, you can press ⌘ + R to execute the current code
Do as Simone Carletti said.
And for editing and saving your file suggest you Scite.
http://www.scintilla.org/SciTEDownload.html here you can download it for many different operating systems.
You get syntax highlighting in a very lightweight editor for almost everything (html, ruby, eruby, xml,...).
But you will need to have at least a Window Manager running.
in ~/.vimrc
autocmd FileType ruby imap <F8> <C-o>:w <CR> <C-o> :!ruby % <CR>
this way you can save and execute your file at once within insertion mode
In vim:
:!ruby %
will execute ruby on the current file. Remember to save it first!
If you are the Emacs type you should check out ruby-mode (which IIRC was written by Matz) and inf-ruby. See e.g. http://lathi.net/pages/emacs-ruby
You don't say what OS you're using, so I'm assuming either Linux or Mac-OS.
When you're at a command-line and using vim (not gvim) you can do a <CNTRL>+Z to temporarily halt the editor and return to the command-line. Issue any commands you need, then use "fg" to return to the editor.
There are times I'll use :!ruby % from inside vim (or gvim) but sometimes I need the real command line and if I'm ssh'd into a machine the <CNTRL>+Z trick is nice.
Agreed with #Simone Carletti. If you are learning the language and want to make sure that methods/classes are doing what you want then you can use irb.
There is a gem called interactive_editor which enable you to run vim inside irb (side-by-side actually). Watch this Vimcast for demo.

Rubygame on OS X shebang problem

I'm playing around with Rubygame. I installed it with the Mac Pack, and now I have the rsdl executable. rsdl game.rb works fine, but when I chmod +x the rb file, add the shebang to rsdl (tried direct path and /usr/bin/env rsdl) and try to execute it (./game.rb), it starts to flicker between the Terminal and rsdl which is trying to open, and eventually gives up and gives a bus error. Anyone know what's causing that? I'm on Snow Leopard (10.6.2) if it makes a difference.
Thanks.
I believe that this is a problem with embedded ruby. Ruby has a horrible hack to handle shebang execution which essentially it looks for "ruby" in the executable name (i.e. argv[0]). If your embedded ruby executable doesn't have "ruby" in its name, this hack fails, and you get an infinite loop (this occurs even with the stock ruby if you rename it). Assuming this is the case here, you can sym-link or mv rsdl to rsdl-ruby (or similar), change your shebang to #!/usr/bin/env rsdl-ruby and it will work.
There may be problems with where rsdl expects you to be executing from (i.e. from the executable, rather than a game file). The way i would find out is popping open the rsdl executable (presuming that it's just a ruby script), and poking around inside. I think it'd be useful also to know why you're trying to execute standalone from your ruby script.

Resources