0
votes

I'm trying to execute ruby code on ubuntu 14.04 and while executing getting error,

'': uninitialized constant BSON::Document (NameError)

The code is,

#!/usr/bin/ruby
require 'uri'
require 'bson'
require 'mongo'
b = BSON::Document.new
b["$query"] = {"token" => {"$gt"=>""}}

payload = b.to_bson[4..-2]
id_ish = ("\n\n" + "a"*24 + "\n\n")

fake_id = "a"*24 +
  "\x02_id\0".unpack('H*')[0] +
  [id_ish.size/2 + 1].pack('V').unpack('H*')[0] + id_ish + "00" +
  payload.unpack('H*')[0]

puts URI.encode(fake_id)

User.find fake_id

I have gone through the documentation, BSON Tutorial

Any idea what i'm missing here. Any pointer would be appreciated.

1

1 Answers

0
votes

It looks like you are trying to execute this code “in vacuum.”

require method will silently return false unless succeeded. Also, to execute the code you need the respective gems to be installed. First of all, run

$ gem install bson
$ gem install mongo

in your shell. Now retry with your script. The gems above should make everything working fine. If not, you need to setup ruby environment, there are many tutorials on that.

There also could be an issue with system/local ruby (the hashbang line dictates to execute the system one.) Try to remove this line and run the script with

$ ruby script_name.rb