any way to create an 'executable' file in ruby so you can run file.rb with ./file
I know you can just run ruby files ruby filename.rb
I also want to be able to send arguments so ./file argument_1 argument_2
run an ordinate ruby file
Add #! /usr/bin/env ruby to your first line of file.rb file.
giving execute permission to this file: $ chmod +x file.rb
run: $ ./file.rb
run ruby file with parameter from commandline:
use ARGV.
#! /usr/bin/env ruby
puts " parameter0 is: #{ARGV[0]}"
puts " parameter1 is: #{ARGV[1]}"
output:
$ ./test.rb a b
parameter0 is: a
parameter1 is: b
doubli click to run .rb file in windows
install ruby in windows
mouse right click .rb file
open with ...
choose ruby program. as the "default program"
for more details, refer to: https://techforluddites.com/windows-10-change-the-default-programs-for-opening-files/
I have a ruby script with following content:
#!/data1/thirdparty/ruby/bin/ruby -I/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/gems/2.0.0/gems/ruby-net-ldap-0.0.4 -I/data1/thirdparty/ruby/lib/ruby/site_ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/2.0.0/i686-linux
When I ran the script it throws the following error:
<internal:gem_prelude>:1:in `require': cannot load such file -- rubygems.rb (LoadError)
from <internal:gem_prelude>:1:in `<compiled>'
I took an strace of the program and found this in the strace:
open("/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/enc/encdb.so", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
So it seems like ruby is not able to handle -I properly because it is including it in the file path itself. How can I force the script to use -I as an include path directive?
Try this one:
#!/bin/bash
exec /data1/thirdparty/ruby/bin/ruby -I/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/gems/2.0.0/gems/ruby-net-ldap-0.0.4 -I/data1/thirdparty/ruby/lib/ruby/site_ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/2.0.0/i686-linux -x "$0" "$#"
#!ruby
p "this is my ruby code"
I would work with rvm and bundler. You can then call your script with a rvm-wrapper (for example if run in a cron job), or with bundle exec for development. You will need to have your *.gemspec well configured (for an example run bundle new mygem and look at mygem.gemspec).
The other possibility would be to write a shell script including
/data1/thirdparty/ruby/bin/ruby -I/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/gems/2.0.0/gems/ruby-net-ldap-0.0.4 -I/data1/thirdparty/ruby/lib/ruby/site_ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/2.0.0/i686-linux <yourfile.rb>
.
Btw. the #! line is called a shebang or hashbang, in case you want to research what happens.
I have a simple test file which contains:
#!/usr/local/bin/ruby -w
puts "Hello, Ruby!";
And when I try running it using ruby test.rb I get this error:
ruby: No such file or directory -- test.rb (LoadError)
Make sure you're running it from the same directory the file is in. If it's in a subdirectory, use ruby subdirectory_name/test.rb. If you use the ls command in your terminal, you should see the test.rb file in the directory you are in now.
run like this
ruby test.rb.txt
I would like to write a CLI application wrapped into a Gem that can be invoked the same way git commands are invoked, or gem commands. Eg when running say "git clone " you don't need to precede it with 'ruby'. However, the tutorials and articles I've seen so far about writing gems, don't show this. The examples either require you to run your gem through irb, with appropriate requires, or you run it like 'ruby '. This is not what I want. If you know of any tutorials that cover this, then that would be great.
Thanks.
The "#!" line at the start of a script tells your shell which executable to execute the script with. In this case, it tells it to find the Ruby executable from the environment and give the script to it for execution.
By means of example, I have a file called "hi", with the following:
#!/usr/bin/env ruby
puts "hi!"
I make it executable:
$ chmod a+x hi
Then I can execute it directly, without explicitly invoking the Ruby interpreter:
$ ./hi
hi!
Per the tutuorial you would simply provide such a file which requires your gem and whatnot, and provide it in the executables property of your gemspec:
Gem::Specification.new do |s|
# ...
s.executables << 'hi'
When the gem is installed, the hi script would be installed into a location discoverable on the path, so you could then invoke it.
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