Ruby on windows with mongodb

setting up ruby for windows, like anything windows these days, is more annoying then the other environments, especially Linux. Here are some key point I hope will save you some time:

  1. Ruby installer for windows, is only the start. it's located here. while you're there, don't stop with the installer: make sure to download the development kit (Development Kit section). You'll need it later on. make sure to extract it in a simple path, e.g. c:\devkit or alike. don't use spaces or special characters.
  2. Once ruby is installed, lets check the gem operation: if you can run gem update –system without an error – great, but if not, here is what you need to do: download the pem file here and save it in your rubygems/ssl_certs/ folder. now, the command should be executed correctly.
  3. lets update the system with



    gem update


  4. To install mongo, lets run



    gem install mongo
    gem install bson_ext


  5. The last one will install bson in C which is much faster. great? sure, but it's not going to work (Windows). so now what? first, lets go to the install folder of this gem (..lib/ruby/gems/[version]/gems/bson_ext[xxx]/ using cmd.
  6. once there open the cbson.c file that is located inside of ext/cbson folder. make sure that you have a reference to winsock2 and not arpa/inet. note that it is existed in more advanced versions, so if it's there, you don't need to change it. This is how it should looks like:


    #ifdef _WIN32
    #include <winsock2.h>
    #include <arpa/inet.h>
    #include <sys/types.h>

    note that if you already have this file, your installation might actually work, so you can skip directly to the test code below.

  7. next, you need to setup your devkit installment, so go to your devkit folder, and run

    ruby dk.rb init

    . this will generate the config.yml file in that folder.

  8. edit this file, making sure that it contains the ruby path at its end, like this: – c:/ruby. note the spaces and the backslash. these are not typos.
  9. next run ruby

    dk.rb install


  10. in your command window that is in the gem folder, run gem build bson_ext.gemspec.
  11. move the new gem c
  12. delete the entire bson_ext gem folder
  13. Run:

    gem install bson_ext-1.11.1.gem --local


  from within the folder you've saved that gem.

Starter code:

require 'rubygems'
puts('testing mongo...')

if you can run this code without an error or a mongo warning claiming that you are not using bson_ext, you are good to go!


File listing command under windows

I got this simple request from someone I know. He wants to list all the files in a specific folder, copy them to a text file, so he can send these to someone else for processing.

Well, this is very easy. We just use the dir command for that, and then we redirect it to a file. For example, lets list all the jpeg files:

dir *.jpg > my_pictures.txt

However, We might have pictures in many formats. So, we would like to include these as well:

dir *.jpg *.png *.jpeg > my_pic.txt

That will do the trick. Note, however, that some header information is still included. We only want the file names, nothing else. So, we use the /b (bare – no headers) switch to achieve this goal:

dir /b *.jpg *.png *.jpeg > my_pic.txt

Now this is a serious progress. But… Lets say that our files are not listed in a single directory. Lets say we have some sub directories that we like to search as well. No problem! we will add the /s (sub directory) switch:

dir /b/s *.jpg *.png *.jpeg > my_pic.txt

That’s All! we can now list any Items in this folder and any folder inside it into a text file. if that is a big file, I suggest compressing it.

Windows/DOS commands

  1. Show the path variable: echo %path%
  2. list directories and files: dir. list one screen at a time: dir /p list item with sizes(recursive): dir /s. the / tags can follow (/s /p)
  3. print characters to the screen: echo
  4. Pause a batch with an option to abort: pause
  5. Copy file: copy
  6. Outputting data to a file instead of the screen (e.g. the dir command) dir >myfile.txt
  7. Delete file(s):del
  8. Display time: time. will also let you change it, so be careful!
  9. Display the date: date. will also let you change it, so be careful!
  10. Show command history:doskey. This is the default for modern Windows versions.
  11. Comparing two files: fc.