Is there a way to convert ruby scripts to executable(linuxmint) - ruby

Hey guys, I run LinuxMint and I have some scripts which I want to run without terminal, can you tell me how?

You need to:
Put as the first line of your ruby script the following shebang line (so bash knows what program to use to run this file)
#!/usr/bin/env ruby
Make your ruby file executable. In a console you can do this:
chmod 700 my_ruby_file.rb
Test that your ruby file is now executable. In a console write:
./my_ruby_file.rb
Notice that we are no longer calling "ruby my_ruby_file.rb", but instead just call the file directly "./my_ruby_file.rb"
If your ruby program executed normally, then everything is going well. If it does not execute and shows you an error, then something is not well done from the previous steps (and post your error)
In your desktop, create an application link, and point it to the my_ruby_file.rb
Once you have the application icon created and shown in your desktop, you can doble-clickit and it will run your application.
Tell us how it went- cheers

Related

OSX Terminal - application listed by 'which' does not run by default

I followed instructions on this page to change the profile of the Mac terminal when I'm running SSH. The short explanation is that it puts a wrapper script in /usr/local/bin that changes the colour then calls /usr/bin/ssh. When I call this script with the full path it works perfectly, but when I call 'ssh', it appears to use the regular application without the wrapper script.
When I call 'which ssh', the result is '/usr/local/bin/ssh'. My PATH variable is '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin', which looks fine for me. The wrapper script has executable permissions ('-rwxr-xr-x ').
What am I missing? Why would the regular ssh be called rather than than the wrapper script, given the 'which' command points to the one that I want?
You forgot to clear bash's program location cache.
hash -d ssh

How do I run this shell script?

I'm using this script:
https://github.com/gilleswittenberg/BackupSql/blob/master/BackupSqlShell.php
But when I run cake BackupSql from my terminal within my console folder I get this error:
Error: Shell class BackupSqlShell could not be found.
I have put BackupSqlShell.php inside app/Console/Command/
Am I doing this right?
You should run shells in 2.x as follows (from the app dir):
Console/cake FooBar
You can run Console/cake which will output what is available. Try it and see if your shell is showing.
If it is, make sure you type it correctly, copy paste to be sure.
If its not, check permissions and location is 100% correct, eg: case and so on.
Edit: You can not, ever, run shells from Console/Command. always from APP dir.

How to execute a Ruby script in Terminal?

I've set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I've even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?
Just call: ruby your_program.rb
or
start your program with #!/usr/bin/env ruby,
make your file executable by running chmod +x your_program.rb
and do ./your_program.rb some_param
Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb
Open terminal
cd /home/User1/program
ruby test.rb
format or test.rb
class Test
def initialize
puts "I love India"
end
end
# initialize object
Test.new
output
I love India
Assuming ruby interpreter is in your PATH (it should be), you simply run
ruby your_file.rb
To call ruby file use : ruby your_program.rb
To execute your ruby file as script:
start your program with #!/usr/bin/env ruby
run that script using ./your_program.rb param
If you are not able to execute this script check permissions for file.
Just invoke ruby XXXXX.rb in terminal, if the interpreter is in your $PATH variable.
( this can hardly be a rails thing, until you have it running. )
For those not getting a solution for older answers, i simply put my file name as the very first line in my code.
like so
#ruby_file_name_here.rb
puts "hello world"
Although its too late to answer this question, but still for those guys who came here to see the solution of same problem just like me and didn't get a satisfactory answer on this page, The reason is that you don't have your file in the form of .rb extension. You most probably have it in simple text mode. Let me elaborate.
Binding up the whole solution on the page, here you go (assuming you filename is abc.rb or at least you created abc):
Type in terminal window:
cd ~/to/the/program/location
ruby abc.rb
and you are done
If the following error occurs
ruby: No such file or directory -- abc.rb (LoadError)
Then go to the directory in which you have the abc file, rename it as abc.rb
Close gedit and reopen the file abc.rb. Apply the same set of commands and success!
In case someone is trying to run a script in a RAILS environment,
rails provide a runner to execute scripts in rails context via
rails runner my_script.rb
More details here:
https://guides.rubyonrails.org/command_line.html#rails-runner
Open Terminal
cd to/the/program/location
ruby program.rb
or add #!/usr/bin/env ruby in the first of your program (script tell that this is executed using Ruby Interpreter)
Open Terminal
cd to/the/program/location
chmod 777 program.rb
./program.rb
You can run ruby code just passing -e option
ruby -e 'x = Time.now; puts x;'
Output will be:
2022-06-22 15:55:06 +0500

Start script on login

I have written a ruby daemon and I would like for it to run when I log in. It is normally run by going to the command line and calling ruby my_ruby_script.rb. How can I start my daemon on login? (Running 10.6 Snow Leopard).
There's an option to add applications etc that need to start at login, you could try writing a shell script or an apple script thing that launches terminal and runs ruby my_ruby_script.rb, or possibly even just add my_ruby_script.rb to this list after adding a #!/bin/env ruby line to the top of that file. http://support.apple.com/kb/HT2602?viewlocale=en_US gives precise instructions as to how to add an application to be started at login.
If you need to use AppleScript to actually start a terminal application (I believe this is not the case, but I am not in front of my mac now and hence can't test), just create an applescript file with something like
do shell script "ruby <path>/my_ruby_script.rb"
Hope this helps
As Panda said, add:
#!/bin/env ruby
to the begining of the file, and then you could add a reference to your file inside ~/.bashrc or ~/.profile or even /etc/profile , depending on your needs.
Check this out: https://stackoverflow.com/questions/3484429/profile-and-bashrc-doesnt-work-on-my-mac/3484472#3484472

How to make OSX application that just runs opens some file type and runs arbitrary Ruby code?

It's trivial to make a program executable from shell - just put #!/usr/bin/ruby on top, chmod +x it and done. Unfortunately OSX won't let me associate file type with such scripts - it requires its .apps instead. This sort of distinction doesn't seem to exist on other operating systems.
What's the simplest way of making such .app, which would merely execute some arbitrary Ruby code?
You could use Automator or create an AppleScript application using "do shell script" to execute your Ruby script.
It seems that Script Editor has "Save as Application Bundle" option, which creates foo.app which I can associate with file types.

Resources