Ruby regex works with ruby command but not shebang - ruby

I have the following 2 regular expressions in a ruby file. They run fine when i use the ruby command but if i try to run via ./apachereport.rb it generates an error.
regex:
urls = parse(#file, /(?<=GET )\S+/)
codes = parse(#file, /(?<=HTTP\/[0-9]\.[0-9]" )\S+/)
error:
./apachereport.rb:34: undefined (?...) sequence: /(?<=GET )\S+/
./apachereport.rb:47: undefined (?...) sequence: /(?<=HTTP\/[0-9]\.[0-9]" )\S+/
The shebang I'm using is as follows, which seems to work fine with other ruby files:
#!/usr/bin/ruby

The most likely explanation for this is that you have multiple versions of ruby installed. The version installed in /usr/bin (which is the one you're using in your shebang line) is 1.8.X, which did not support ?<= (look-behind) in regexen. The one you execute when you type ruby apachereport is probably ruby 1.9, which does support ?<=.
To verify that this is the case type in which ruby and notice that it prints something other than /usr/bin/ruby and/or compare the results of /usr/bin/ruby --version to ruby --version.

Related

"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.

OSX 10.9.4 ruby -version error

Not sure what happen do my ruby install
$ ruby -version
ruby 1.8.7 (2013-12-22 patchlevel 375) [i686-darwin13.3.0]
-e:1: undefined local variable or method rsion' for main:Object (NameError)
You can string multiple parameters together when you run Ruby. In this case, -v is being interpreted as 'version' where as the 'e' is being interpreted as -e, which from the man page:
Specifies script from command-line while telling Ruby not to search the rest of the arguments for a script file name.
Ruby is then trying to parse the remainder ('rsion') as an argument to the -e. What you want it either:
ruby -v
or
ruby --version

Ruby script: shebang with absolute path to ruby not working

I am using Mac OS X. I have two versions (2.1.5 and 2.0.0) of Ruby installed. The former installed at /another/.path/to/ruby (there is a dot before "path" to mimic the fact that the path contains a dot-headed directory in between), in addition to the default system one (version 2.0.0) at /usr/bin/ruby. I used rbenv to install Ruby.
After I manually set the PATH environment variable so the default ruby command will be found in another directory: /another/.path/to/ruby. Now I check
which -a ruby
It is using correct ruby first, as output.
/another/.path/to/ruby
/usr/bin/ruby
Now I create a script, rbs, with the first line of shebang specifying the ruby to use.
#!/usr/bin/env ruby
puts 'hey there'
Then I run
./rbs
it outputs 'hey there'. Good. Meanwhile, the Ruby is using the correct version.
/usr/bin/env ruby --version
as well as
ruby --version
both output 2.1.5. So it does great job to use the new version.
However, here is where the problem occurs: now I update rbs file to be:
#!/another/.path/to/ruby
puts 'hey there'
Note that I updated the shebang to use the absolute path to the desired ruby. then I run
./rbs
It outputs:
./rbs: line 2: puts: command not found
which is too weird;
but if I run
ruby ./rbs
it outputs 'hey there' as normal. It looks like the shebang works perfect using /usr/bin/env ruby, but not for absolute path for the newly install ruby?
Why is this? Is there a way to fix it so the updated script can still work by typing the following?
./rbs
Thanks!
The puts: command not found message indicates that your script is not being run by Ruby, but by the shell instead. So first, I would double-check your shebang line's syntax and path.
Second, note that rbenv uses shims that dynamically find and run the right version of ruby (and associated programs like gem, etc). But the shims are scripts, and scripts can't themselves be shebang interpreters; you have to find and use the actual path to the ruby executable (as output by rbenv which ruby).
On the other hand, since /usr/bin/env is an executable, you can always use something like #!/usr/bin/env ruby, which will work even if the ruby it finds in the path is itself a script.
I can't comment, (otherwise I'd add as a comment) but I think its worthwhile to add that the
#!/usr/bin/env ruby
MUST be the first line of the file. This tripped me up for a while.
source

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