Nokogiri won't load in script - ruby

I'm using Ruby for the first time and have to process XML files. Nokogiri appears to be the best way to accomplish this, but I am doing something wrong. When I load in IRb things work great:
$ irb
2.1.0 :001 > require 'nokogiri'
=> true
2.1.0 :002 > exit
but when I attempt the same thing from a script I get an error:
$ ./sample.rb
./sample.rb:3:in `require': no such file to load -- nokogiri (LoadError)
from ./sample.rb:3
$ cat sample.rb
#!/usr/bin/ruby
require 'nokogiri'
What is different in the script environment that prevents loading?

You might want to try using #!/usr/bin/env ruby instead of #!/usr/bin/ruby since IRB might be using a different Ruby than the one located at /usr/bin/ruby.

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.

How to run external script with Ruby?

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

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)

Ruby gem to change irb output font-size and color?

Can any body guide me to ruby gem that changes font-size and color of irb terminal output dynamically. If not gem is there any other method to lighten up terminal and/or irb?
Also you can try to use pry. It's alternative to irb shell. It has colorized results out of the box and much more.
There is RailsCast about it.
I asked DuckDuckGo and it told me that you should look at: fancy_irb
$ sudo gem install fancy_irb
$ irb
> require 'fancy_irb'
=> true
> FancyIrb.start
=> "Enjoy your FancyIrb :)"

Are ruby command line switches -rubygems & -r incompatible?

I recently converted a ruby library to a gem, which seemed to break the command line usability
Worked fine as a library
$ ruby -r foobar -e 'p FooBar.question' # => "answer"
And as a gem, irb knows how to require a gem from command-line switches
$ irb -rubygems -r foobar
irb(main):001:0> FooBar.question # => "answer"
But the same fails for ruby itself:
$ ruby -rubygems -r foobar -e 'p FooBar.question'
ruby: no such file to load -- foobar (LoadError)
must I now do this, which seems ugly:
ruby -rubygems -e 'require "foobar"; p FooBar.question' # => "answer"
Or is there a way to make the 2 switches work?
Note: I know the gem could add a bin/program for every useful method but I don't like to pollute the command line namespace unnecessarily
-rubygems is actually the same as -r ubygems.
It doesn't mess with your search path, as far as I understand, but I think it doesn't add anything to your -r search path either. I was able to do something like this:
ruby -rubygems -r /usr/lib/ruby/gems/myhelpfulclass-0.0.1/lib/MyHelpfulClass -e "puts MyHelpfulClass"
MyHelpfulClass.rb exists in the lib directory specified above.
That kind of sucks, but it at least demonstrates that you can have multiple -r equire directives.
As a slightly less ugly workaround, you can add additional items to the ruby library search path (colon delimited in *nix, semicolon delimited in windows).
export RUBYLIB=/usr/lib/ruby/gems/1.8/gems/myhelpfulclass-0.0.1/lib
ruby -rubygems -r MyHelpfulClass -e "puts MyHelpfulClass"
If you don't want to mess with the environment variable, you can add something to the load path yourself:
ruby -I /usr/lib/ruby/gems/1.8/gems/myhelpfulclass-0.0.1/lib \
-rubygems -r MyHelpfulClass -e "puts MyHelpfulClass"
Note: this problem exists for ruby 1.8, but is resolved in ruby 1.9.
On 1.8, if you specify both libs via -r, ruby will try to load each library without paying attention to changes in the $LOAD_PATH. But rubygems does change $LOAD_PATH so the gems can be found.
The reason it works with irb is that irb does pay attention to $LOAD_PATH changes.
Unfortunately, the best workaround I've found is to use the more verbose form:
ruby -rubygems -e 'require "foobar"; p FooBar.question'
The pain doesn't increase linearly with the number of libs though, if you use an iterator:
ruby -rubygems -e '%w(rake rspec).each{|r| require r }'

Resources