Ruby Resources


  • irb - interactive ruby command line
  • gem - a Ruby library, packaged up for easy inclusion in other projects.
  • rails - a web application framework that “put Ruby on the map”
  • bundler - a gem dependency management system
  • capistrano - a command line tool for remote SSH commands against multiple servers


I would highly recommend the following Ruby and Rails books:

I haven’t read this, but Francis may very well be interested in it:


The Ruby community is traditionally focused on automated testing. Commit to writing tests.

You will:

  • write better code.
  • gain a deeper understanding of your code.
  • express what your code is doing.
  • better be able to adapt to changes.
  • be able to reduce the chance of regressions.

And paradoxically, you will develop skills to write software solutions faster. Also be mindful that if it is hard to test, it is quite possible that it will be very hard to work with.

Think of your tests as an important letter to a future developer. That future developer may be you; or the new developer joining your team. This letter should be as clear and concise as possible.

Your test suite is just as valuable as your production code, perhaps more so. It verifies that what you have written for production actually works. It can provide reassurances as you upgrade your software.

There are several types of tests that you could end up writing:

  • Unit tests - testing a singular class with little to no interaction with other classes
  • Functional tests - testing the interaction of a set of closely related objects
  • Integration tests - testing the systems behavior with minimal knowledge of its underworkings

Testing Resources

  • Capybara - “helps you test web applications by simulating how a real user would interact with your app.”
  • WebMock - “Library for stubbing and setting expectations on HTTP requests in Ruby.”
  • Timecop - “A gem providing “time travel” and “time freezing” capabilities”
  • Guard - “A command line tool to easily handle events on file system modifications”

Tasks at Hand

Now with all that out of the way, on to the tasks.

Search LDAP

From Ruby Net::LDAP

require 'rubygems'
require 'net/ldap'

ldap = :host => server_ip_address,
     :port => 389,
     :auth => {
           :method => :simple,
           :username => "cn=manager, dc=example, dc=com",
           :password => "opensesame"

filter = Net::LDAP::Filter.eq("cn", "George*")
treebase = "dc=example, dc=com" => treebase, :filter => filter) do |entry|
  puts "DN: #{entry.dn}"
  entry.each do |attribute, values|
    puts "   #{attribute}:"
    values.each do |value|
      puts "      --->#{value}"

Create a file

File from Ruby Standard Lib

# Write "Hello World" to the specified path:"./path/to/my/file.txt", 'wb') { |file|
  file.puts "Hello World"

Write a CSV file from Ruby Standard Lib (Ruby 1.9.3 or greater)"path/to/file.csv", "wb") do |csv|
  csv << ["row", "of", "CSV", "data"]
  csv << ["another", "row"]
  # ...

SSH/SCP a file to the server

Invoke command line methods

`scp ./path/to/my/file`

From Net::SSH

require 'net/ssh'

Net::SSH.start('host', 'user', :password => "password") do |ssh|
  # capture all stderr and stdout output from a remote process
  output = ssh.exec!("hostname")

  # capture only stdout matching a particular pattern
  stdout = ""
  ssh.exec!("ls -l /home/jamis") do |channel, stream, data|
    stdout << data if stream == :stdout
  puts stdout

  # run multiple processes in parallel to completion
  ssh.exec "sed ..."
  ssh.exec "awk ..."
  ssh.exec "rm -rf ..."

  # open a new channel and configure a minimal set of callbacks, then run
  # the event loop until the channel finishes (closes)
  channel = ssh.open_channel do |ch|
    ch.exec "/usr/local/bin/ruby /path/to/file.rb" do |ch, success|
      raise "could not execute command" unless success

      # "on_data" is called when the process writes something to stdout
      ch.on_data do |c, data|
        $STDOUT.print data

      # "on_extended_data" is called when the process writes something to stderr
      ch.on_extended_data do |c, type, data|
        $STDERR.print data

      ch.on_close { puts "done!" }


  # forward connections on local port 1234 to port 80 of
  ssh.forward.local(1234, "", 80)
  ssh.loop { true }

Simple web-app

Ruby on Rails Getting started

…to query a database and return records

# Creates a new rails application, using sqlite
$ rails new APP_PATH

# Creates a new rails application, using postgresql
$ rails new APP_PATH -d postgresql

# Use the scaffold to get kick the tires
$ rails generate scaffold Blog title:string content:text

…to return a JSON response

class BlogController
  def index
    @blogs = Blog.all
    respond_to do |format|
      format.json # Add this line