How to run external script with Ruby? - ruby

I am new to Ruby and am learning from this tutorial on the Ruby site.
I can run simple scripts from the IRB command line but I am not sure how to run a script I have written on an external .rb file.
Is there a special directory I must put this in to run it from IRB?

You should not run scripts in files using IRB. Exit the IRB and run:
ruby some_path/some_script.rb

require 'my_script.rb'
No special directory is required.

Use require './filename.rb'. For example:
06:34:38 ~$ echo "puts 'asdf'" > foo.rb
06:34:55 ~$ irb
2.0.0p247 :001 > require './foo.rb'
asdf
=> true
2.0.0p247 :002 >

eval(File.read 'your_script.rb')
no special directory, just make sure to use correct path

Related

Run RSpec for more than one Ruby version using single command

I was looking for a way to run 'rake spec' for all ruby versions.
So far I wrote this code in Rakefile but it doesn't work:
RUBIES = ['ruby-2.0.0', 'ruby-2.1.1', 'ruby-2.2.1']
RUBIES.each do |ruby_v|
sh "rvm use #{ruby_v}"
RSpec::Core::RakeTask.new(:spec)
end
Any ideas how may I accomplish this?
It would be great if I could pass down arguments specifying whether I want to run the tests for a single ruby version or for all.
EDIT:
Here's the Rakefile I used and the error I am getting. It's not the same file which I mentioned at the top. It uses rvm use #{ruby_v}; rspec spec as suggested by Keith in the answer.
rvm is a shell command and its Ruby setting applies only to the current instance of the shell. So when you run sh "rvm use #{ruby_v}" you're changing the rvm version of the shell that you have invoked, and then exiting from that shell.
In addition, your Ruby program is an operating system process running the Ruby virtual machine, so when you make the call to RSpec::Core::RakeTask.new(:spec), you're still in that same OS process and Ruby version with which you started your script.
You'll need to run the rspec tests in the shell that you invoke and change the Ruby version on. I thought something like this would work:
`rvm use #{ruby_v}; rspec spec`
...but as you pointed out, it does not. You need to run the new shell as a "login shell" so that rvm is set up correctly. In addition, the new shell must be told that the thing you're invoking is a shell command and not a script or binary executable. In other words:
1) have your command explicitly invoke bash or zsh (sh did not work for me on my Mac).
2) specify (probably with -l) that it is a login shell.
3) specify (probably with -c) that you are executing a shell command (rvm) and not a script or executable.
I am using zsh as my shell, but you should be able to substitute bash in the code below and it should work (and of course put your rspec command in there):
2.3.0 :024 > puts `zsh -lc "rvm current"`
ruby-2.3.0
=> nil
2.3.0 :025 > puts `zsh -lc "rvm use jruby; rvm current"`
Using /Users/kbennett/.rvm/gems/jruby-9.0.5.0
jruby-9.0.5.0
=> nil
2.3.0 :026 > puts `zsh -lc "rvm current"`
ruby-2.3.0
=> nil

"require" command not found when require 'nokogiri'

Homebrew, ruby 2.0.0p648, nokogiri 1.6.7.2 are installed. When require 'nokogiri' there appears an error:
-bash: require: command not found
What's wrong?
Try running the script from the console/terminal shell like this:
ruby script_name.rb
You can also try adding this shebang line to the top of your .rb file:
#!/usr/bin/env ruby
This will auto-identify the script as Ruby when you try to run it directly in some shells. Also see:
Why is it better to use “#!/usr/bin/env NAME” instead of “#!/path/to/NAME” as my shebang?
what is the use of “#!/usr/local/bin/ruby -w” at the start of a ruby program
You're running your command in bash -- this is a Ruby command.
You can't run a Ruby command directly in bash. If you want to use Ruby at command line, open the Ruby shell irb.
$ irb
and then you'll see the prompt
2.3.0 :001 >
The first number indicates the Ruby version you are using. In my case it is Ruby 2.3.0. The second number is the command number.
Then you may type
require 'nokogiri'
and it surely work, if you have this gem installed.

Nokogiri won't load in script

I'm using Ruby for the first time and have to process XML files. Nokogiri appears to be the best way to accomplish this, but I am doing something wrong. When I load in IRb things work great:
$ irb
2.1.0 :001 > require 'nokogiri'
=> true
2.1.0 :002 > exit
but when I attempt the same thing from a script I get an error:
$ ./sample.rb
./sample.rb:3:in `require': no such file to load -- nokogiri (LoadError)
from ./sample.rb:3
$ cat sample.rb
#!/usr/bin/ruby
require 'nokogiri'
What is different in the script environment that prevents loading?
You might want to try using #!/usr/bin/env ruby instead of #!/usr/bin/ruby since IRB might be using a different Ruby than the one located at /usr/bin/ruby.

Gem not available in self executing ruby file

I have written myself a simple Ruby script that requires the Listen Gem. Running it in the console like this works perfectly.
$ ruby script.rb ARGS
I was always passing the PWD as a argument. I got annoyed of that so I wanted to make the script executable across my linux. So I added the following line to the beginning of my script
#!/usr/bin/env ruby
require "listen"
...
When I now use it as a self executing file, it runs into an error.
$ ./script.rb
Output:
./listen.rb:55: uninitialized constant Listen (NameError)
from ./script.rb:3:in `require'
from ./script.rb:3
Do you have any suggestions what the cause of this problem could be? I have one guess: #!/usr/bin/env ruby calls a different ruby env that $ ruby actually is. But how can I find that out? (I am using rbenv to manage my rubys)

How to run Ruby code from terminal?

I need to run a few lines of Ruby code from terminal, but I can't find the needed parameter for it.
Can you explain how to do this?
If Ruby is installed, then
ruby yourfile.rb
where yourfile.rb is the file containing the ruby code.
Or
irb
to start the interactive Ruby environment, where you can type lines of code and see the results immediately.
You can run ruby commands in one line with the -e flag:
ruby -e "puts 'hi'"
Check the man page for more information.

Resources