Prototype

I’m reading a paper called [Metaprogramming in Ruby – A Pattern Catalog] by Sebastian Gunther and Marco Fischer. They gave some code example, that I did not understand properly in the beginning. Hence, this post aims to clarify the mystery of those lines.

This part of code is from Figure 11, the implementation of Prototype module and its usage inside Hydra class. I modified the code to add some output to see how the code is executed.

module Prototype
  def self.included ( base )
    puts "#{self} included in #{base}"

    base.class_eval do
      puts "#{self}.class_eval is called"

      def initialize
        puts 'Prototype Initialize Called'
        raise " InstantiationError "
      end
    end

    base.instance_eval do
      puts "#{self}.instance_eval is called"

      def create
        puts "create() is called, self = #{self}"
        prototype = self.clone

        prototype.instance_eval do
          puts "prototype.instace_eval() is called"

          def initialize
            puts 'prototype.instance_eval.initialize() is called'
            raise " InstantiationError "
          end
        end

        prototype.instance_eval { def is_prototype?; true ; end }

        puts "... returning a cloned prototype #{prototype}"
        return prototype
      end
    end
  end
end

When we include a module Prototype in a class, we can see the execution. The function included is called when Prototype is included in Hydra. Inside that function, the class_eval and instance_eval do their jobs. `

class Hydra
  include Prototype
end

===> We get
Prototype included in Hydra
Hydra.class_eval is called
Hydra.instance_eval is called

We cannot initiate an instance from Hydra class dge

hydra = Hydra.create

===> We get
create() is called, self = Hydra
prototype.instace_eval() is called
... returning a cloned prototype #<Class:0x007feadd8429f0>

Looking a bit deeper

irb(main):093:0> hydra.methods false
=> [:initialize, :create, :is_prototype?]
irb(main):094:0> hydra.instance_methods false
=> []
irb(main):107:0> hydra
=> #<Class:0x007feadd8429f0>
irb(main):109:0> hydra.included_modules
=> [Prototype, Kernel]

And it’s getting weird here. So Hydra.create is returning cloned object of Hydra instead of an instance of Hydra. And that cloned object, in return, also have a class method create.

irb(main):115:0> Hydra.create.create.create
create() is called, self = Hydra
prototype.instace_eval() is called
... returning a prototype #<Class:0x007feadc8240a0>
create() is called, self = #<Class:0x007feadc8240a0>
prototype.instace_eval() is called
... returning a prototype #<Class:0x007feadd8fbc70>
create() is called, self = #<Class:0x007feadd8fbc70>
prototype.instace_eval() is called
... returning a prototype #<Class:0x007feadd8fb888>
=> #<Class:0x007feadd8fb888>

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
  ...
end

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
end
end

class << Foo
  def speak; end
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
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

locale

Output:

LANG=
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

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.

http://www.recursion.org/2016/6/19/displaying-emoji-in-git-log

 

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)
        provider.save!
      end
    end

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)
        provider.save!
      end
    end