How to turn on STDOUT.sync in ruby from the command line - ruby

I have objective-C code that calls ruby scripts and monitors STDOUT. However, ruby does not seem to synchronise STDOUT by default, so I need to put STDOUT.sync = true at the beginning of the script to see output as it happens.
Can I do this as a command line option when calling a ruby script?

You can create a setup file to require before your script. Then call ruby with the -r flag:
ruby -r "$HOME/.rubyopts.rb" myscript.rb
You can also set the environment variable RUBYOPT to automatically include that file every time you run ruby:
export RUBYOPT="-r $HOME/.rubyopts.rb"

Related

Running Ruby scripts in Atom or command shell

complete Ruby amateur here. After playing around with Ruby on various interactive online coding environments, I thought I'd try to set up Ruby on my Windows in the same way I've set up Python (using Atom and Terminal-Plus). After being frustrated and Googling for answers the past 3 hours, I thought I'd bring my question here.
Using Python, I can save a file (test.py) in Atom, and execute it using Terminal-Plus by typing:
py -i test.py
And this would create an interactive shell where I can call any functions I've stored inside my test Python script. I've learnt that I can do a similar thing with Ruby (for a test file test.rb) using:
ruby -r test.rb
but this yields the following error:
C:/Ruby21- x64/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such
file -- test.rb (LoadError)
from C:/Ruby21-x64/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
The same thing happens if I use cmd prompt to try and execute the script. I've seen other problems like this where the person was missing a key RubyGem that they were trying to call, but my test.rb file is literally:
def xyz
for i in 1..100
puts i
end
end
I'm just trying to call a Ruby script and be able to call the functions I've stored in the script in the console, in this case xyz.
-r command line switch is used to use require to load lib as a library before executing.
Try
ruby test.rb
from inside the directory this file is located, it should work.

how to build wrapper script

Sort of an odd question, but: how would one go about creating a wrapper shell script that can be used in the #! line in other scripts.
wrap.sh
#!/bin/bash
set -e
echo "wrapper!"
exec ruby "$#"
test.rb
#!/usr/bin/env wrap.sh
puts RUBY_VERSION
puts "the ducks come from the trucks"
wrap.sh is in the path, and test.rb is marked as executable.
Now I do:
./test.rb
wrapper!
ruby: no Ruby script found in input (LoadError)
The goal is to execute the ruby script via the wrapper (The ruby version can be either local or comes from a traveling ruby install that is shipped along with the app).
As far as I can tell ruby is invoked, it's just unhappy with the #! in the test.rb and refuses to run the script. I cannot remove the #! because that's how the script is executed in the first place.
Any workarounds for this?
So, I cannot use rbenv/rvm/etc. There is more logic in the wrapper than this, but this is the gist of it.
Looks to me like the arguments are not being passed to Ruby in "$#". I don't think the bang-hash line is the problem.
I don't see anything in your script which actually passes the contents of test.rb to wrapper.sh, which is the bigger issue.
Perhaps the real problem can be solved by some other means? For example, is the problem you're trying to solve to run arbitrary commands prior to the invocation of any Ruby script from the command line? Perhaps it can be approached that way...
It looks like Ruby just checks that the hash-bang line contains "ruby": https://github.com/ruby/ruby/blob/v2_2_2/ruby.c#L1580 So basically having ruby somewhere in the #! line is all it takes.

Ruby: Keep console open after script execution

I wrote a Ruby script like the following example. The basic functionality is the same:
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
The problem is when I run the script it prints the solution I want, but the console window closes right after. I want the console to keep open.
I'm currently on windows, but the solution should be working on every system.
One way is to run the ruby script with a .bat file and pause it, like so:
ruby script.rb
PAUSE
I hope there is a way without the additional .bat file. Does Ruby has a function like PASUE integrated?
It seems like you double click the ruby script file.
Instead issue the following command in cmd shell.
ruby filename.rb
If you don't want that, you can add gets to the end of the script.
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
gets # <----
But this is not recommended because .. if you run the command in cmd shell or terminal you should type extra Enter to return to the shell.
Use the -r options of irb.
irb -r ./filename.rb

Interaction with shell through Ruby continuously?

I want to capture output from shell commands so I am using
response = `#{command}`
which is fine if you want to run only one command and not a continuous interaction. For instance if I do
response = `cd tmp`
# response = '', which is correct
response = `ls`
I would like it to return ls for within tmp, since in the previous command I had changed directory to temp. Is there a way to run a continuous shell on its own Thread or a gem or something to that effect?
The ` starts a sub-shell, so it does not affect your current Ruby shell. However you can use Ruby's Dir.chdir or FileUtils.cd to change the working directory of your Ruby shell.
Btw, maybe you like fresh, which is a hybrid between a system and a Ruby shell. You can normally use cd/ls there, while being in a Ruby shell.

Automatically require a class when running / starting ruby

I am doing some monkey patching in some of Ruby classes and I would like that to be included AUTOMATICALLY whenever I run ruby.
For example:
I added the method trim to String. I want to be able to do this:
ruby -e 'puts " aaaa ".trim'
I don't want to do this:
ruby -e 'require "monkey.rb"; puts " aaaa ".trim'
Is there anyway to include my monkey patches evertime I start ruby? How about irb?
Thanks!
ruby and irb both take a -r option that lets you specify a library to load when running those executables. If you want to automatically load your monkey.rb library, you can start ruby with the invocation $ ruby -r monkey (assuming monkey.rb is in your $RUBYLIB path. If you don't want to do that each time, you can set up an alias in your shell config file. For example (in Bash), you could add:
alias ruby='ruby -r monkey'
irb is probably the place where you can do this most simply. When using irb, you can use an initialization file to store anything you want run on every startup. In your home directory ("cd ~"), create a file called ".irbrc", and drop in your "require 'monkey.rb'" statement, that should do it. From then on when you start up irb, it will run anything in that script first.

Resources