Can't create MongoClient.new - ruby

I don't know what the deal is with this. Everything works fine until i try to create a new MongoClient
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'mongo'
=> true
irb(main):003:0> include Mongo
=> Object
irb(main):004:0> client = MongoClient.new('127.0.0.1', 27017)
NameError: uninitialized constant MongoClient
from (irb):4
from :0
irb(main):005:0>

You are using an older version (1.5.2) of the mongo gem instead of the most current one (1.8.0). That days the connection has been made by
conn = Mongo::Connection.new
Find out more in the documentation of version 1.5.2

Related

undefined method `symbolize_keys' after requiring active support.

I am trying to symbolize the keys of a hash in a non rails project. I can see the symbolize_keys method is part of Active Support so I imported the library but it still doesn't work.
Here is an example of it failing
2.4.2 :001 > require 'active_support'
=> true
2.4.2 :002 > {'test' => 'test'}.symbolize_keys
NoMethodError: undefined method `symbolize_keys' for {"test"=>"test"}:Hash
Expected output
{test: "test"}
You should require 'active_support/all' if you want active support core extensions also required:
2.3.4 :002 > require 'active_support/all'
=> true
2.3.4 :003 > {'test' => 'test'}.symbolize_keys
=> {:test=>"test"}

"NameError: uninitialized constant UserAgent" when using Watir

I'm new to ruby, I seem to have successfully installed watir-webdriver and webdriver-user-agent gems, but when I was trying to follow the instructions here I've stumbled. How to proceed?
>> require 'watir-webdriver'
=> true
>> require 'webdriver-user-agent'
=> true
>> driver = UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
NameError: uninitialized constant UserAgent
from (irb):3
from /usr/bin/irb:12:in `<main>'
Try using Webdriver::UserAgent

Unable to open excel with Roo or Spreadsheet Gems

When I try this with roo gem:
irb(main):001:0> require 'roo'
=> true
irb(main):002:0> oo = Excel.new("C:/Users/Abash/Desktop/test1.xls")
**NameError: uninitialized constant Excel** from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
When I try this with spreadsheet gem:
irb(main):001:0> require 'spreadsheet'
=> true
irb(main):002:0>Spreadsheet.client_encoding = 'UTF-8'
=> "UTF-8"
irb(main):003:0> book = Spreadsheet.open 'C:/Users/Abash/Desktop/test1.xls'
**Errno::EACCES: Permission denied** - C:/Users/Abash/Desktop/test1.xls
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/spreadsheet-0.8.2/lib/spreadsheet.rb:69:in `open'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
Can somebody show me a work around for these errors?
I believe you are using ruby 1.9 or above version. In such a case,you need to specify the gem while creating a new instance.
require 'roo'
s =Roo::Excel.new("myspreadsheet.xls")
s =Roo::Excelx.new("myspreadsheet.xlsx")

Trying to run Hash.to_xml, but Ruby complains?

I'm just trying to run a very simple snippet on my RVM Ruby 1.9.2 installation:
require 'rubygems'
require 'active_support'
obj = {"foo" => "bar"}
xml = obj.to_xml
Ruby complains as follows:
NoMethodError: undefined method `to_xml' for {"foo"=>"bar"}:Hash
from (irb):2
from /Users/.../.rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>'
Why is this happening? Isn't to_xml a method of Hash?
require this:
require "active_support/all"

MongoId - uninitialized constant App::Mongo (NameError)"

In Sinatra app I have
#config.rb
require 'mongoid'
class App
configure do
Mongoid.configure do |config|
name = "my_db"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.slaves = [Mongo::Connection.new(host, 27017, :slave_ok => true).db(name)]
config.persist_in_safe_mode = false
end
end
end
#Gemfile
gem "mongoid", "~> 3.0.0"
gem "bson_ext"
and it gives me an error "`const_missing': uninitialized constant App::Mongo (NameError)"
How do I fix it?
This is due to the fact that Mongoid 3.x no longer uses the 10Gen Ruby driver so Mongo module will not be loaded by require 'mongoid'. You need to use the new Mongoid.load! method. You may have to change your config.yml file a bit as the syntax has changed. Please see http://mongoid.org/en/mongoid/docs/installation.html

Resources