No such file or directory -- test.rb (LoadError) - ruby

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

Related

Executable file in ruby

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/

Terminal issue - ruby: no such file or directory

Total newb here. I'm trying to get a simple ruby program to run in the terminal on my MacBook Pro. I used Atom text editor to write the following:
class Sample
def hello
puts "Hello, World!"
end
end
s = Sample.new
s.hello
I saved the file as my_program.rb to a folder on my desktop. I go to the terminal to run the program. I type
ruby my_program.rb
and it returns
ruby: No such file or directory -- my_program.rb (LoadError)
I can use the irb and run a single line of ruby using
ruby -e 'puts "hello world"'
But can't get it to find the .rb file.
I appreciate any help y'all can offer! Thanks!
ruby ~/Desktop/my_program.rb
Ruby might be clever, but is has no mind-reading builtin. You need to tell it, where your file is. This is, of course, not Ruby-specific, but applies to all commands - they can't guess, where in your file system you have stored a file.
An alternative would be to place ~/Desktop in your PATH and use
ruby -S my_program.rb
Ruby will then execute the first program with this name which it finds in $PATH. Whether it is wise to place the Desktop directory into the PATH is a different issue....

how to distribute a ruby script via homebrew

How can I deploy a simple ruby script via homebrew?
Here's what I tried
Wrote formula in a GitHub repo named homebrew-foo
# file https://github.com/foo/homebrew-foo/blob/master/foo.rb
class Foo < Formula
desc "A command line tool"
url "https://github.com/foo/foo/archive/master.zip"
version "5.0.1"
def install
bin.install "foo"
lib.install Dir["lib/*"]
end
end
The other repository contains the ruby script. These are the files
./foo
./lib/libfile1.rb
here's what the script does
#!/usr/bin/env ruby
require './lib/libfile1.rb'
puts "came here"
The problem is that the require fails.
$ brew install foo/foo/foo
$ foo
results in this error
/Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in
require': cannot load such file -- ./lib/libfile1.rb (LoadError)
from
/Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in
require' from /usr/local/bin/foo
$ which foo
/usr/local/bin/foo
I suspect it's because the .rb file is not there at /usr/local/bin/foo/lib/libfile1.rb
Any ideas whats the proper way to do this?
There are two issues with your script:
The first one is you try to require some file relatively to the current directory; i.e. the one from which the script is run, not the one it’s located in. That issue can be fixed by using Ruby’s require_relative:
#!/usr/bin/env ruby
require_relative './lib/libfile1.rb'
puts "came here"
The second issue is the script assumes the lib/ directory is located in its directory; which it’s not because your formula installs the script under <prefix>/bin/ and the library files under <prefix>/lib/. Homebrew has a helper for that use-case called Pathname#write_exec_script. It lets you install everything you need under one single directory, then create an executable under bin/ that calls your script.
Your formula now looks like this:
class Foo < Formula
desc "A command line tool"
url "https://github.com/foo/foo/archive/master.zip"
version "5.0.1"
def install
libexec.install Dir["*"]
bin.write_exec_script (libexec/"foo")
end
end
It installs everything under libexec/ (lib/ is usually reserved for lib files), then add an executable under bin/ that calls your libexec/foo script.
I found the answer to my own question, actually it's a technique used by someone on the net, basically do something like this
#!/usr/bin/env ruby
DBMGR_HOME = File.expand_path('../..', __FILE__)
$LOAD_PATH.unshift(File.join(DBMGR_HOME, 'lib'))
require 'dbmgr'
And the recipe can be like this:
https://github.com/callahanrts/homebrew-dbmgr/blob/master/dbmgr.rb

Something is wrong with `#!`(shebang)(hashbang) in ruby script

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.

Execution of ruby file fails from within shell script

I'm trying to use a .rb file from within a shell script like so:
ruby file.rb "input data"
In file.rb (it's in the root of a rails app), it requires another file which is throwing an error when I try the chmod method. Any suggestions?
Doing ruby file.rb "input data" outside of the .sh file works completely fine.
I've tried the answers here: Run .rb (Ruby) file on a Shell Script, and chmod and adding #!/usr/bin/ruby do not work. Would be grateful for any suggestions.
I'm on a mac, ruby-2.1.4.
When I try to run it, I get:
"/Users/user/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler/setup (LoadError)"
How about replacing ruby file.rb "input data" with
bundler exec ruby file.rb "input data" ?
You should also check bundle-exec manpage for more information on how bundler will run your script.

Resources