Unexpected token error with Ruby - ruby

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

Related

Can't generate ruby exe using ocra due to ARGV[0]

Running the command ocra script.rb --no-autoload --no-enc --add-all-core gives me the error initialize: can't convert nil into String (TypeError) for the following line:
doc = Nokogiri::XML(File.open(ARGV[0]))
Whats going on here? I want to build the executable to be able to take any argument and use that file as the xml configuration.
It seems a long time but the accept solution doesn't work for me.
The working solution is adding -- then any fake data to your argument to make the execution flow to be just as normal
example for:
so you need to do
ocra yourscript.rb -- ANYDATAHERE
Just add this above that line:
exit if defined? Ocra
# skip anything below this line when we're building the exe
Unless there's a require or otherwise loaded dependency below that line you should be fine.

Ruby cmd line script executes code non-consecutively

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.

Rspec command line argument not supported?

My website will be tested in different languages and browser (Firefox, Chrome).
I use Ruby, watir-webdriver and rspec.
How do I write tests with Ruby, watir and rspec , that I can specify command line arguments ?
Example: rspec testfile.rb 1 2
1 => language finnland (load XML file)
2 => firefox
rspec gives me an error message for this example code:
argOne = ARGV[0].to_i
argTwo = ARGV[1].to_i
rspec can't process the command line argument?
Thank you for your help
According to this post on the ruby forum, you cannot pass argument to rspec, but you can pass variable from the command line via the environment variables
command line ( on Linux / bash )
LANG=1 BROWSER=2 rspec testFile.rb
command line ( on Windows )
set LANG=1
set BROWSER=2
rspec testFile.rb
in your testFile.rb
argOne = ENV["LANG"].to_i
argTwo = ENV["BROWSER"].to_i

sh: 1: ruby: not found when i am executing ruby from browser

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);

net/ftp invalid argument error

I have a relatively simple script that was working. Nothing in the script changed and I'm using ruby 1.8.6.
require 'net/ftp'
ftp = Net:: FTP.new(ip)
ftp.login(user=name,passwd=pass)
ftp.chdir(pathHere)
ftp.gettextfile('onhandapt.txt', File.basename('onhandapt.txt'))
ftp.close
I know from running through the steps in irb that I can login successfully, and even issue a ftp.list command to get the current directory, but ftp.chdir is where the scripts fails. In irb, the ftp.chdir command yields 'nil'. Double checked the path on the server.
The script produces this error:
c:/ruby/lib/ruby/1.8/net/ftp.rb:211:in readline': Invalid argument (Errno::EINVAL)
from c:/ruby/lib/ruby/1.8/net/ftp.rb:211:ingetline'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:221:in getmultiline'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:235:ingetresp'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:251:in voidresp'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:274:invoidcmd'
from c:/ruby/lib/ruby/1.8/monitor.rb:242:in synchronize'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:290:insendport'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:298:in makeport'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:329:intransfercmd'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:421:in retrlines'
from c:/ruby/lib/ruby/1.8/monitor.rb:242:insynchronize'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:419:in retrlines'
from c:/ruby/lib/ruby/1.8/net/ftp.rb:518:ingettextfile'
Ideas welcome.
make sure pathHere contains a valid pathname and not set to nil
Figured it out. We had some office network changes. I now need to use passive mode, which can be set with this command: ftp.passive=true

Resources