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
Related
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
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.
The rvm command lets you tell it what environment you want to use and pass a block to it to invoke a script or something (for a one-time run of the command) e.g.:
rvm 1.9.2-p290#whatever-gemset do ruby my-script.rb
However, if the script accepts command line arguments as well and you try to pass them script at invocation, rvm complains. Does anyone know if rvm has syntax to support/allow this?
e.g.:
rvm 1.9.2-p290#whatever-gemset do ruby my-script.rb -p
ERROR: Unrecognized command line argument(s): '-p' ( see: 'rvm usage' )
To avoid rvm trying to parse those parameters as arguments to rvm itself, put them in quotes to pass them as a single argument:
rvm 1.9.2-p290#whatever-gemset do "ruby my-script.rb -p"
The only reason this would fail is if some kind of shell-expansion is being done before this command is actually executed which is non-standard behavior.
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.
With most (all?) Ruby interpreters one can write ruby -we "..." and have the supplied Ruby code executed (with warnings).
With RVM one can write rvm ruby foo.rb and have the source file executed against all installed interpreters.
Is there a way with RVM to run a one-off line of code against all interpreters? I have tried executing both rvm ruby -we "..." and rvm -we ruby "..." to no good effect.
See rvm exec.
rvm exec ruby -we 'puts "#{`which ruby`.strip} #{RUBY_VERSION}"'