I am creating a PHP website which have to execute a ruby command from terminal for that i am using :-
1)
$output = shell_exec('ruby emailConverter.rb 2>&1');
var_dump($output);
This throws the error
string(23) "sh: 1: ruby: not found"
or
2)
exec("ruby emailConverter.rb", $output, $return);
var_dump($output);
UPDATE
AFTER doing this as #psal suggested
$path = '/home/vishal/.rvm/rubies/ruby-2.1.2/bin/ruby'
$output = shell_exec($path.' emailConverter.rb 2>&1');
var_dump($output);
getting this error
string(308) "/home/vishal/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require: cannot load such file -- premailer (LoadError)
from /home/vishal/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require'
from emailConverter.rb:4:in `<main>'
but when i execute this any of the above command(1 and 2) directly from terminal, it works fine...
what is the problem with that?
Any help?
In my case,
I added
#!/usr/bin/env /usr/local/rvm/wrappers/ruby-2.4.1/ruby
on top of the ruby script file.
ie:
#!/usr/bin/env /usr/local/rvm/wrappers/ruby-2.4.1/ruby
require 'jwt'
...
As #simonwo said, this error happend because Ruby is not in your PATH. Find the ruby binary using which then add it to your script. Usually it's in /usr/bin/.
Example on Ubuntu :
$ which ruby
/usr/bin/ruby
Once you have the path, add it to your shell_exec :
$rubyBin = '/usr/bin/ruby';
shell_exec("$rubyBin emailConverter.rb 2>&1");
// or
exec("$rubyBin emailConverter.rb", $output, $return);
Related
I've built a ruby script that is supposed to run in the terminal.
$ ruby script.rb
I have some code specific to newer versions of ruby so I added a ruby version check towards the top of the page:
abort("You're using ruby #{RUBY_VERSION}. Please use version 2.1 or newer") if (RUBY_VERSION.to_f < 2.1)
I double checked the code line in irb and seems to work when changing the ruby version via RVM.
However, when I run the ruby script file under, say ruby 1.8.7, the script blows up with the following error:
$ ruby script.rb
script.rb:6: odd number list for Hash
option1: 'some options',
^
script.rb:6: syntax error, unexpected ':', expecting '}'
option1: 'some options',
^
script.rb:6: syntax error, unexpected ',', expecting $end
This would be expected behavior if I didn't have the version check on the top of the file.
Why doesn't the version check execute before the next lines of code? Is there a way to force execution of the ruby check before continuing with the rest of the code?
My full file is:
#!/usr/bin/env ruby
abort("You're using ruby #{RUBY_VERSION}. Please use version 2.1 or newer") if (RUBY_VERSION.to_f < 2.1)
options = {
option1: 'some options',
option2: 'some more options',
option3: 'other options'
}
That error is on the ruby parser. In ruby 1.8.7 hashes with symbols must be written with hashrockets { :option => 'some options'} because the shorthand { option: '' } was only introduced in ruby 1.9
To explain it better, ruby has to parse the whole file before executing anything on it. So your version check wont be executed because your file has invalid ruby 1.8 syntax.
I'm trying to reference a file to write to it using RubyMine and I'm having trouble figuring it out. When using the full path, the code errors out while running it with RubyMine.
When I use the same code and run it in terminal, the code works fine using the command:
ruby studio_game players.csv
How can I get the file to be recognized without having to designate the full path in RubyMine?
Erring Code:
require_relative 'player'
require_relative 'game'
player1 = Player.new("moe")
player2 = Player.new("larry", 60)
player3 = Player.new("curly", 125)
knuckleheads = Game.new("Knuckleheads")
knuckleheads.load_players(ARGV.shift || 'players.csv')
Error message:
/Users/MNickey/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) "/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game"
/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `readlines': No such file or directory - players.csv (Errno::ENOENT)
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `load_players'
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game:9:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
knuckleheads.load_players(ARGV.shift || 'players.csv') is almost certainly what is causing the problem. Unless you have configured the launcher for the script with a command line argument, ARGV will be empty and you will be getting plain-old 'players.csv' as the result.
I'm pretty certain you are running the script with an argument when you run it in the command line.
To set a command line parameter in RubyMine, go to Run / Edit Configurations, find the launch configuration for your script, and add the path to your csv file to the Script Arguments input box.
I am an absolute beginner in Ruby. I created a small ruby file, and it runs well when I run the command ruby "methods.rb". That means I am in the correct directory.
But when I launch irb and run the command require "methods.rb", I get the following response:
LoadError: cannot load such file -- methods.rb
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:53:in `require'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:53:in `require'
from (irb):1
from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
Ruby doesn't add the current path to the load path by default.
From irb, you can try require "./methods.rb" instead.
I do have a ruby file called so.rb in the directory /home/kirti/Ruby. So first from IRB I would change my current working directory using Dir#chdir method. Then I would call #load or #require method. My so.rb file contains only p hello line.
I would go this way :
>> Dir.pwd
=> "/home/kirti"
>> Dir.chdir("/home/kirti/Ruby")
=> 0
>> Dir.pwd
=> "/home/kirti/Ruby"
>> load 'so.rb'
"hello"
=> true
>> require './so.rb'
"hello"
=> true
To add the directory you are executing the ruby script from to the load path use:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), '' ) )
or if you have put your dependencies in 'subdir' of the current directory:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'subdir' ) )
If you are going to load things in IRB that are in your current directory, you can do:
irb -I.
Note the 'dot' there, indicating current directory.
If you are exploring and making changes in that file, while you are in IRB, use load rather than `require as load lets you load your changes, and require will only allow the file to be required once. This means you will not need to exit IRB to see how your changes are being affected.
To find out what options you have for IRB, you can do irb --help which is good to do if you are learning the tool.
Attempting to execute the basic.rb example for HTTParty. Running into an interesting error. Executing this under 1.8.7 on my Mac (10.7.2). When I run the example (see code below), I get this error:
$ ./HTTPartyTest.rb
./HTTPartyTest.rb: line 1: syntax error near unexpected token `('
./HTTPartyTest.rb: line 1: `dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))'
If I take line 1 and execute it via irb I get this result.
>> dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
=> "/Users/me/Workspaces/lib"
Not sure why this occurring. Any help is appreciated.
You probably need to add the correct hash-bang header or this will be executed using your shell instead:
#!/usr/bin/env ruby
# ... (Rest of program)
The alternative is to explicitly specify you want to run it with Ruby:
ruby ./HTTPartyTest.rb
I have a Ruby script that is trying to require the restclient module. When I reduce it down to just this one line, it still fails:
#!/usr/bin/env ruby
require 'restclient'
When I run it, I get the following error:
./test.rb:3:in `require': no such file to load -- restclient (LoadError)
from ./test2.rb:3
When I run irb, the module loads fine:
$ irb
>> require "restclient"
=> true
>>
As far as I can tell, it looks like both the script and irb have the same module paths:
$ ruby -e "puts $:"
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
$ irb
>> puts $:
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
=> nil
>>
What would cause a module to load through irb, but not when run directly through Ruby?
One other confusing detail is that the restclient gem doesn't seem to be in my path to start with. How is irb finding it?
$ locate restclient | grep gems
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/bin/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/abstract_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/exceptions.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/net_http_ext.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/payload.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/raw_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/resource.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/spec/restclient_spec.rb
Thanks - Marc
Try
require "rubygems"
in the source code file, or starting the ruby program with ruby -rubygems filename.rb.