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.
Related
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
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
which ruby - /usr/local/bin/ruby
.bash_profile has the above path
I have not left any trailing spaces before #!/usr/bin/env ruby in the ruby script
When I try to run sh ruby_script, it gives require: command not found. It is not able to recognize any of the ruby commands.
I checked all the other posts getting the same error, and fixed everything accordingly. What else could I be missing?
Running sh ruby_script is wrong. You must run ruby ruby_script instead. If the file is executable run ./ruby_script.
sh ruby_script is invoking the shell. Try either
/path/to/ruby_script
or
ruby /path/to/ruby_script
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.
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.