`<main>': undefined local variable or method `database' for main:Object (NameError) - ruby

I want to connect MongoDB with Ruby.
I've used the following Ruby file:
require 'rubygems'
require 'mongo'
#con = Mongo::Connection.new
#db = #con['tutorial']
#users = #db['users']
When I use the command ruby connect.rb I get this error:
undefined local variable or method 'database' for main:Object (NameError)

As clearly specified in the documentation, the connection should be established that way:
#con = Mongo::Client.new('mongodb://127.0.0.1:27017/tutorial')
#users = #con[:users]

Related

Figure out where a method was defined

If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)

undefined method data_for for main:object error in Ruby

I wrote a small script that try to read data from a .yml file using DataMagic.
require 'watir-webdriver'
require 'data_magic'
DataMagic.yml_directory="/home/krishna/RUBY/"
DataMagic.load "testdata.yml"
testData = data_for("testdata.yml/Testcase_01")
puts "#{testData[:username]}"
when i execute this i am getting an error
hashdata.rb:7:in `<main>': undefined method `data_for' for main:Object (NoMethodError)
Tell me what i am missing. Thanks.
require 'watir-webdriver'
require 'data_magic'
include DataMagic
DataMagic.yml_directory="/home/krishna/RUBY/"
testData = data_for("testdata/Testcase_01")
puts "#{testData['username']}"

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"

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Jruby: NameError: uninitialized constant Neo4j

I ran this example from git: https://github.com/andreasronge/neo4j
require "rubygems"
require 'neo4j'
Neo4j::Transaction.run do
node = Neo4j::Node.new(:name => 'andreas')
node.outgoing(:friends) << Neo4j::Node.new(:name => 'peter')
node.outgoing(:friends).each {|node| puts "name #{node[:name]}"}
end
and it outputted the error:
NameError: uninitialized constant Neo4j
const_missing at org/jruby/RubyModule.java:2626
(root) at ./neo4j.rb:4
require at org/jruby/RubyKernel.java:1027
require at ./neo4j.rb:36
(root) at /Users/ZATLUKE/RubymineProjects/nokogiri/neo4j.rb:2
load at org/jruby/RubyKernel.java:1052
(root) at -e:1
Any ideas?

Resources