View gem source code

Viewing a gem source code

  1. Simple way, without installing anything new
    EDITOR=[your editor] bundle open [name of gem]
  2. With a gem called gem-open
gem install gem-open
# Open the gem source code right in the terminal
gem open rspec
# Open source code with your editor of choice
gem open -e subl rspec
# We can also generate our own rdoc
gem rdoc rspec
# and then view it
gem server



Week 45

Running rubocop on changed files Really nice article describing about how to use rubicon when developing new features

# before committing
git ls-files -m | xargs ls -1 2>/dev/null
# before pushing
git diff-tree -r --no-commit-id --name-only master@\{u\} head | xargs ls -1 2>/dev/null

Week 30

I didn’t realize how fast time flies….


Sinatra explained by Zhengjia: great tutorial, dive deep into how Sinatra works.

Let’s Build a Sinatra – by Gabe Berke-Williams He is showing how to build a Nancy – a similar type of Sinatra – from scratch. I can now understand how to build the Classic type

Almost Sinatra in 6 lines of code

How to enable sessions with Sinatra: overview of the defaul Sinatra session, then Rack::Session::Cookie, and the last Rack::Session::Pool


To provide syntatically sugar for Sinatra Classic style

How to make AutoAligner work in OSX

I’m using AutoAligner (GitHub) to do some alignment things in Sublime. However, after installing the default key (ctrl+k, ctrl+a) doesn’t work.

It turns out that the repository doesn’t have the file Default (OSX).sublime-keymap

You can add this file by the following:

  1. Preferences -> Browse Package
  2. Duplicate the file `Default (Linux).sublime-keymap` and rename to Default (OSX).sublime-keymap



book = 'A more beautiful question'
author = 'Warren Berger'

Select those 2 lines in Sublime, then press ctrl+k, followed by ctrl+a

book   = 'A more beautiful question'
author = 'Warren Berger'


git clean

The documentation for git clean is clear, but still it’s long. So here comes the summary

git clean -n # Dry run, show list of untracked files to be deleted
git clean -n -x # Dry run, show list of untracked + ignored files to be deleted

git clean -f # Remove untracked files

git clean -x -f # Remove untracked + ignored files

The order of middlewares matter

Despite having working with Ruby for more than 6 months, I have never really understood the usage of middlewares inside a Rack application.

We are working on a problem to set the X-Request-Id header for the incoming request, and we would like to have that header in the response as well.

So for setting the header, we use a middleware layer, named RequestId. Initially I thought we would need another middleware to set a header for a response as well, but no, we do not need it.

A request is coming to Rack app with its set of headers. Then whenever we modify the headers for the Rack app

This is the sample response from Rack app. It’s an array with 3 elements:

  • an Integer: HTTP status code
  • a Hash: Some headers
  • an Array: The response body
map <span class="hljs-string">'/posts'</span> <span class="hljs-keyword">do</span>
run Proc.<span class="hljs-keyword">new</span> { |env| [<span class="hljs-string">'200'</span>, {<span class="hljs-string">'Content-Type'</span> => <span class="hljs-string">'text/html'</span>}, [<span class="hljs-string">'first_post'</span>, <span class="hljs-string">'second_post'</span>, <span class="hljs-string">'third_post'</span>]] }

Each Rack app is required to return the array with 3 elements. Hence once we have a middleware to modify the response of Rack app, then it’s there.

To read more: Proper Rack Middleware Ordering | Verbose Logging – this is really useful website that help me to understand how the chain of middlewares are interacting with each other.