RSpec basics

Recently I have re-studied the RSpec. This is just my reminder.

  • describe
    • to be used to group something to be described. can be nested.
  • context
    • same as describe but usually used to list some different conditions
  • it
    • to be used to write test case. If it's a test for models or controller, it should have only one expectation.
  • expect
    • to be used to describe an expectation. Several operators(to, not_to, etc) and matchers(be, eq, etc.) are available.
  • subject
    • to be used to declare a test object which can be used implicitly after here - the following its sentence uses this to check the description method.

An easy example is below:

class Computer
  attr_accessor :cpu, :memory, :disk, :has_too_big_cpu

  def initialize(cpu, memory, disk)
    raise 'Non integer values given' unless
      cpu.is_a? Integer and memory.is_a? Integer and disk.is_a? Integer
    @cpu = cpu 
    @memory = memory
    @disk = disk

    @has_too_big_cpu = true if cpu > 10
  end 

  def description
    return "CPU: #{@cpu}, Memory: #{@memoory}, Disk: #{@disk}";
  end 
end

describe 'Computer' do
  describe 'initialization' do
    context 'with non-integer values' do
      it 'will raise an exception' do
        expect { Computer.new(1, 1, '10GB') }.to raise_error
          'Non integer values given'
      end 
    end 

    context 'with too big CPU' do
      it 'will set has_too_big_cpu' do
        computer = Computer.new(15, 1, 1)
        expect(computer.has_too_big_cpu).to be true
      end 
    end 
  end 

  subject { Computer.new(1, 1, 10) }

  its(:description) { should eq "CPU: 1, Memory: , Disk: 10" }
end

Interested articles (3/May/2014)

Daily chart: Crowning the dragon | The Economist

This article said that China will become the largest economy by the end of the year against the past prediction of 2019.

Dragdis - Drag & drop anything anywhere.

An amazing web service. With a browser extension, we can store texts, images, webpages, videos and so on very easily by just doing "drag and drop". This introduction movie can help you understand what you can do.

http://youtu.be/HLEAA_tIpbY

I hope Evernote acquires this service ASAP!

Google Shows How Its Self-Driving Cars Are Getting Smarter With 700K Miles Driven | TechCrunch

I've predicted for a long time that the next IT trend would be the car industry. This could be an evidence for it. Interesting.

How to Drink All Night Without Getting Drunk | Healthy Living - Yahoo Shine

This is just what I need. (I'm not a big drinker. This can also be used for lightweight drinkers, I guess.)

Collaboration between google map and reCAPTCHA

I think the collaboration is useful and works well. The original article is here. This is interesting for me.

Google Online Security Blog: Street View and reCAPTCHA technology just got smarter

The normal CAPTCHA is a web technology which you can often see when you login to some websites. It is often some distorted characters with some lines or dots which is possible for human to read but for computers. This is usually used for prevent 'bot' program from attempting sign up web services mechanically to create bunch of IDs or something else.

On the other hand, AFAIK, reCAPTCHA is almost the same as the CAPTCHA but it provides 2 words, the one was already known by the server and the other wasn't known - it's too complicated to be identified by machines.

Once a user types these two words in a login form, the server uses the one which is already known to identify whether the user is human or bot, and stores the other to it's database. The word will be shown to several users and the data input by them will also be stored in the database. Eventually servers can identify the unknown word from collected data.

These words analysis seems to be used for google map. And it is used for digitizing existing books according to this.

recaptcha

I like this kind of "killing two birds with one stone".

Several articles I read recently

I'll show you some articles which I recently read and felt something against.

Tarsnap - Why is 1 GB 10^9 bytes instead of 2^30?

I've not known this before. This is not essential, but something new for me was the unit of 'Gm'. We rarely say this, do we? For example, I think the diameter of Earth would be expressed as 12,742 km, not 12Gm.


Updated: The French Move To Protect Workers From After-Hours Email | Fast Company | Business + Innovation

I bet this would not be acceptable in Japan lol
However, I'm working in UK now and I could feel the difference of working style between Japan and UK. Now I think this kind of idea is important because competitions between companies would put on speed in theory unless there are no restrictions. It sometimes makes not only employees but also employers unhappy. My manager is keen to this point. He always attempts to make his employees not to work hard. He said, hard works make people get tired and it finally might cause his leaving from their job.

Rubyにおけるインスタンス変数 クラス変数 クラスインスタンス変数(Instance variables, class variables and class instance variables in ruby)

(English follows)

Rubyにおけるインスタンス変数 クラス変数 クラスインスタンス変数に関するメモ。以下の記事に対するコメントを含んでいる。

includeとextendの違いについて - shoutm's diary

結論は以下の通り。

  • インスタンス変数はオブジェクトに属する
  • クラス変数はクラス階層に属する
  • クラスインスタンス変数は該当クラス(モジュール)のみに属する。
module MyModule
  @@class_val = 2;
  @classinstance_val = 3;

  def mod_func1
    @instance_val = 1;
  end

  def self.mod_func2
    puts @classinstance_val # => 3
  end
end


class MyClass
  include MyModule

  def func1
    mod_func1
    puts @instance_val      # => 1
  end

  def self.func2
    puts @@class_val        # => 2
  end

  def self.func3
    puts @classinstance_val # => nil
  end
end

print 'instance variable = '
a = MyClass.new
a.func1

print 'class variable = '
MyClass.func2

print 'class instance variable = '
MyModule.mod_func2

print 'class instance variable from MyClass = '
MyClass.func3

出力結果は以下の通り。

instance variable = 1
class variable = 2
class instance variable = 3
class instance variable from MyClass = 

特にMyModuleをincludeしたMyClassにおいて、ポイントは以下の通り。

  • MyModuleのクラス変数はMyClassで引き継がれる。(includeしたモジュールがクラス階層に含まれる。)
  • self.func3の時点でselfはMyClassであるため、MyModuleのクラスインスタンス変数は見えない。

==============================

My note for instance variables, class variables and instance class variables of ruby. This also refers to my past blog post:

includeとextendの違いについて - shoutm's diary

In conclusion, we can say as below:

  • Instance variables belongs to an object.
  • Class variables belongs to a class hierarchy.
  • Class instance variables belongs to a class itself.

(See sample code and output results above.)

Especially there are two points with regards to MyClass which includes MyModule.

  • Class variables in MyModule are inherited to MyClass.
  • self(current class) is MyClass at the point of 'self.func3', MyClass can't see the class instance variables in MyModule.

Reijiro - An English learning tool

I've found a sleek open source software for English study.

Reijiro: 間隔反復学習で英辞郎を「読む」アプリ
knsmr/reijiro · GitHub

Have you heard of "Spaced repetition"? According to wikipedia:

Spaced repetition is a learning technique that incorporates increasing intervals of time between subsequent review of previously learned material in order to exploit the psychological spacing effect.


Spaced repetition - Wikipedia, the free encyclopedia

Reijiro is a software which implements this method. We can use it by buying a CD of Eijiro*1 and installing this software into your PC. It's written in Ruby on Rails so it might be difficult if you don't have any knowledge of it.

However, this is very useful for me because I always forget the English words that I have learnt... I think we need to study something continuously when we learn something. It's difficult to manage progress only using a vocabulary list or a dictionary.

I've already installed it but the CD has not arrived yet. It will be delivered in a few days so I think I can review it next week.