Firstly, you have to install MongoDb gem:
gem install mongo
Then run code:
require 'rubygems' # not necessary for Ruby 1.9
require 'mongo'
db = Mongo::Connection.new.db("mydb") # OR
db = Mongo::Connection.new("localhost").db("mydb") # OR
db = Mongo::Connection.new("localhost", 27017).db("mydb")