What I learned this week [3]

A. Ruby Metaprogramming: class_eval and instance_eval

class_eval creates an instance method
instance_eval creates a class method

B. Eigenclass

Integralist has a exceptional article about Eigenclass

Eigenclass methods is accessible by class <<. It is a class containing the singleton methods, and class methods.

First example: define a singleton method for an instance.

class Foo; end
foo = Foo.new
class << foo

Second example: define a singleton method for a class. Actually class Foo is an instance of class Class. And foo is an instance of class Foo

There are 4 ways to create a class method:

  1. Use self.method
  2. Use Class Open technique inside a class definition class << self
  3. Use Class Open technique outside of class definition class << Foo
  4. Use Foo.instance_eval
class Foo
def shout; end

def self.whistle; end

class << self
def sing; end

class << Foo
  def speak; end

Foo.instance_eval {
def talk; end
Foo.methods false
# [:speak, :talk, :sing]

Foo.instance_methods false
# [:shout]

And there are 2 ways to create an instance methods

class Bar
def fly; end

Bar.class_eval {
def soar; end

Bar.instance_methods false
# [:fly, :soar]


Setting locales so that terminal in Mac OS would display Unicode character




We can set the LANG variable by

export LANG=en_US.UTF-8

Or you can also add that line into the file ~/.bash_profile, and then source ~/.bash_profile

Different ways to output Christmas tree [from fileformat] in terminal:

perl -CS -E 'say "\x{1F384}"'
echo -e "\xF0\x9F\x8E\x84"

To display Christmas tree in git log

Turn out that because less program is not up-to-date in Mac OS. Hence we need to update it.



Grape Error Formatter

I was working with Grape API, and I use a custom error formatter. Since we are working on generating error objects according to JSON API specification format:

Here is an official example [source]

HTTP/1.1 422 Unprocessable Entity
Content-Type: application/vnd.api+json

  "errors": [
      "status": "422",
      "source": { "pointer": "/data/attributes/first-name" },
      "title":  "Invalid Attribute",
      "detail": "First name must contain at least three characters."

d But we also want to add

So even though the field jsonapi is not required, we would like to have it in our response. So our response would be like this:

"jsonapi": {
"version": "1.0"
  "errors": [
      "status": "422",
      "source": { "pointer": "/data/attributes/first-name" },
      "title":  "Invalid Attribute",
      "detail": "First name must contain at least three characters."


FactoryGirl create vs. build

Problem: we have the before_save callback for a provider, and we want to test whether this callback is executed correctly.

The test passes

    context 'valid provider' do
      let(:provider) { FactoryGirl.build(:provider) }

      it 'get triggered when saved' do
        expect(provider).to receive(:test_before_save_callback)

Even when we force the creation of provider with let, the test still passes.

    context 'valid provider' do
      let!(:provider) { FactoryGirl.build(:provider) }

      it 'get triggered when saved' do
        expect(provider).to receive(:test_before_save_callback)


What I learned this week [2]

A. I’m reading

JQuery Fundamentals: after realizing that I don’t know the syntax of jquery well enough, I decided to self-educate myself on that matter. And I made a decision to start by reading a book, to at least have the basic knowledge.

B. Something new

Using the localization in jquery-ui-datepicker

//= require datepicker-it when written in a *.js file, that line is first parsed by Sprocket in Rails. This line tells rails to preload some external javascript files.

And in order to use a datepicker, we need to include all the languages that we’re going to use.

New gems

spreewald: from makandra, predefine most frequently used cucumber steps.

consul: from makandra again, to restrict the access right to data.

&& vs. andin ruby

use && for the task when you need only the boolean output. && has high precedence.

The and usage is more complicated, in a sense that it connects two parts of the evaluation. Use and in control flow. and has low precedence.

False value

Ruby: false, nil

Javascript: false, NaN, 0, empty string, undefined. In short, this article says, only object is truthy.

C. Something to remember

  • To deploy the code, vagrant ssh, and then bundle exec cap playground deploy. Since we need the ssh private key, and we can only have access to that key while we are still inside the local environment. Deploying from the terminal in vagrant would not work.

My mistakes