Ruby MongoClient: Initializing from irb succeeds, initializing from script fails - ruby

I'm trying to initialize a MongoClient from a script but I am failing.
What I simply do is:
require 'mongo'
include Mongo
client = MongoClient.new("localhost", 27017)
and it fails with the following error:
/var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/cursor.rb:43:in `initialize': undefined method `connection' for nil:NilClass (NoMethodError)
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/db.rb:564:in `new'
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/db.rb:564:in `command'
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:591:in `block in check_is_master'
from /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:590:in `check_is_master'
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:474:in `connect'
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:698:in `setup'
from /var/lib/gems/1.9.1/gems/mongo-1.9.1/lib/mongo/mongo_client.rb:155:in `initialize'
from ./test.rb:52:in `new'
from ./test.rb:52:in `<main>'
But, when I try the exact same thing from irb, it works.
Ruby version is:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
irb is:
/usr/bin/irb1.9.1 --version
irb 0.9.6(09/06/30)
I also tried requiring 'rubygems', but from what i know, Ruby 1.9 doesn't need it.
Am I forgetting something?

This issue was resolved by re-installing ruby. I never found out what the problem was and what was causing it.
Thanks for everyone's help.

Related

Ruby: Gem dbd-pg(0.3.9) not working with Ruby 2.7

I'm trying to migrate very old script to ruby 2.7, gem seems to be installed correctly but its throwing out error i am assuming that error is because the script is expecting PG gem version to be 0.9.0 but for some reason on ruby 2.7 this is version is not getting installed and its installing 1.2.3 and dbd-pg calls pg gem so that might be causing this issue.
here is the trace:
Trace: C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/dbd-pg-0.3.9/lib/dbd/pg/database.rb:81:in `rescue in initialize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/dbd-pg-0.3.9/lib/dbd/pg/database.rb:40:in `initialize'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/dbd-pg-0.3.9/lib/dbd/Pg.rb:157:in `new'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/dbd-pg-0.3.9/lib/dbd/Pg.rb:157:in `connect'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/dbi-0.4.4/lib/dbi/handles/driver.rb:33:in `connect'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/dbi-0.4.4/lib/dbi.rb:148:in `connect'
C:/Virtuoz/analytics/setup/setup_analytics_databases.rb:212:in `check_database_exists'
C:/Virtuoz/analytics/setup/setup_analytics_databases.rb:222:in `check_already_set_up'
C:/Virtuoz/analytics/setup/setup_analytics_databases.rb:71:in `set_up_databases'
C:/Virtuoz/analytics/setup/setup_analytics_databases.rb:404:in `block in <main>'
C:/Virtuoz/analytics/setup/setup_analytics_databases.rb:386:in `each'
C:/Virtuoz/analytics/setup/setup_analytics_databases.rb:386:in `<main>'.
PS: I never worked on Ruby so any input is welcome. This is first time we are working with ruby and we are not much familiar with the errors or call stacks.
Thanks in advance

Irb Error: undefined method 'split'

I recently opened up irb, and now all of a sudden every time I run irb it gives me this error:
load error: /Users/addisonbean/.rvm/rubies/ruby-2.1.2/.irbrc
NoMethodError: undefined method `split' for nil:NilClass
/Users/addisonbean/.rvm/scripts/irbrc.rb:45:in `<top (required)>'
/Users/addisonbean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/Users/addisonbean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/Users/addisonbean/.rvm/rubies/ruby-2.1.2/.irbrc:11:in `<top (required)>'
/Users/addisonbean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/irb/init.rb:236:in `load'
/Users/addisonbean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/irb/init.rb:236:in `run_config'
/Users/addisonbean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/irb/init.rb:19:in `setup'
irb(main):001:0>
But as I've shown you it still gives me the prompt after all the errors
This is all the code inside of /Users/addisonbean/.rvm/rubies/ruby-2.1.2/.irbrc (the numbers are line numbers)
8 if ENV["rvm_path"].nil?
9 require File.join(ENV["HOME"], "irbrc")
10 else
11 require File.join(ENV["rvm_path"], "scripts", "irbrc")
12 end
And I have no ~/.irbrcw
I would love some help on this, thanks!
Looking at line 45 of rvm's scripts/irbrc.rb, we can find that split being called on path. It would seem that for some reason, your path is nil - probably because one of those rescue nil blocks is running.
I wouldn't advise hacking around the internals of rvm, but it looks like you can fake this by setting the environment variable $rvm_ruby_string, as irbrc.rb checks that before it tries to figure it out. Setting it to "2.1.2" should match what it calculates.
These steps solved my problem:
find the current ruby version
ruby -v
example:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
remove this version
rvm remove 2.1.2
install again
rvm install 2.1.2

Ruby Error: undefined method `start_with?' Using Net::SSH.start

The ruby version I'm using is 1.8.6
I installed gem "net/ssh" using command "gem install net-ssh"
But on running the following script
require 'rubygems'
require 'net/ssh'
HOST = '10.102.5.100'
USER = 'user'
PASS = 'test'
Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
result = ssh.exec!('stat ssl')
puts result
end
Error:
/usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:106:in `load': undefined method `start_with?' for "*":String (NoMethodError)
from /usr/local/rubygems/lib/rubygems/custom_require.rb:36:in `partition'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:106:in `each'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:106:in `partition'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:106:in `load'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:81:in `foreach'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:81:in `load'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:63:in `for'
from /usr/local/rubygems/lib/rubygems/custom_require.rb:36:in `inject'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:62:in `each'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:62:in `inject'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh/config.rb:62:in `for'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh.rb:239:in `configuration_for'
from /usr/local/rubygems/gems/gems/net-ssh-2.9.1/lib/net/ssh.rb:182:in `start'
from TestSSH.rb:9
Why do I get this error?
As the comments on your question point out, the problem arises because Ruby 1.8.6 doesn't have the String#start_with? method. The best solution would be to upgrade to a newer Ruby.
That said, according to the net-ssh README, versions prior to 2.5.1 support Ruby 1.8.x. A visual inspection of the file lib/net/ssh/config.rb as of net-ssh version 2.5.0 confirms that it doesn't use the start_with? method, so you might try that one. Be sure to read the changelog to make sure that the features you need are present in the version you use.

Unable to open the Firefox browser using cmd prompt in watir

My system configurations Windows 7, IE8
when I try to open the firefox browser I got the following error can anyone help me how to resolve these.
C:\Users\khan>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'watir-webdriver'
=> true
irb(main):003:0> e=Watir::Browser.new :firefox
NoMethodError: undefined method `ascii_only?' for {}:Hash
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/json_pure-1.4.3/lib/json/pure/generator.rb:393:in `to_json'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/multi_json-1.3.7/lib/multi_json/adapters/json_common.rb:11:in `dump'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/multi_json-1.3.7/lib/multi_json.rb:115:in `dump'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/json_helper.rb:23:in `json_dump'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:254:in `write_prefs'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:253:in `each'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:253:in `write_prefs'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:252:in `open'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:252:in `write_prefs'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:232:in `update_user_prefs_in'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/profile.rb:79:in `layout_on_disk'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/launcher.rb:58:in `create_profile'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/launcher.rb:34:in `launch'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/firefox/bridge.rb:20:in `initialize'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/driver.rb:31:in `new'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/common/driver.rb:31:in `for'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver.rb:67:in `for'
from D:/Ruby_1.8.7/lib/ruby/gems/1.8/gems/watir-webdriver-0.6.2/lib/watir-webdriver/browser.rb:46:in `initialize'
from (irb):3:in `new'
from (irb):3
from ♥:0irb(main):004:0>
Based on the exception, you are using an older version of selenium-webdriver and watir-webdriver. It is possible that there is an incompatibility between your selenium-webdriver gem and your Firefox version.
Therefore, I would suggest upgrading your gems:
gem install selenium-webdriver
gem install watir-webdriver
It might also be possible that one of the dependencies is also incompatible (eg json-pure where the exception occurs). You might want to trying updating everything.
gem update
You should try to update gem,I am not sure about it,but try once.
gem update --system

Error in requiring Sinatra gem

I'm having a hard time getting Sinatra running on my local setup, Ubuntu Karmic 9.10. The error getting thrown when I have require 'sinatra' is:
NoMethodError: undefined method `[]' for nil:NilClass
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:891:in `compile'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:883:in `gsub'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:883:in `compile'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:856:in `route'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:838:in `get'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:1077
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:929:in `configure'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra/base.rb:1076
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/lib/ruby/gems/1.8/gems/sinatra-1.0/lib/sinatra.rb:4
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from (irb):2
from :0
I've tried:
Uninstalling/reinstalling Sinatra
Updating all gems
Ensuring all dependencies exist (rack)
Any ideas? Your time and help is greatly appreciated!
This is an issue apparently only coming up with 1.8-head on Linux. It is related to regexp matching. If you take a look in the code: path does match /((:\w+)|[\*#{special_chars.join}])/, but $2 is nil, even though it is supposed to be a String.
I recommend switching your ruby version for now.

Resources