L’àmbit de self a Ruby

Com a llenguatge de programació Orientat a Objectes (OO), Ruby té el concepte de self, que fa referència al propi objecte. És important tenir en compte que self fa referència a l’objecte, no a la classe, de manera que allò que representa és diferent en funció de si l’utilitzem en el context de la classe o d’una instància.

I com podríem accedir a la referència a la classe, des d’una instància?

Accessing instance variables in Ruby

Classes may have instance variables that are not explicitly in the public interface – this is, they don’t have a corresponding reader method. These variables can still be accessed with instance_variable_get, a method of Object class. Consider the following example:


If we make an instance of Example and call #name, we’ll get a NoMethodError, since there is actually no name method.

We can get the value of @name with instance_variable_get:

The returned value will be that of the current state of the variable when we retrieve it:

Rails, and its things

Today I forgot my laptop at home and had to do an emergency installation of Ruby, Git and Rails on my sister-in-law’s (yes, this is a real story). After all the thing, I finally ran gem install rails. That’s the list of all the dependencies that were installed by Rails. I just thought it’d be funny to post it here.

$ gem install rails

Fetching: thread_safe-0.3.4.gem (100%)
Successfully installed thread_safe-0.3.4
Fetching: minitest-5.4.2.gem (100%)
Successfully installed minitest-5.4.2
Fetching: tzinfo-1.2.2.gem (100%)
Successfully installed tzinfo-1.2.2
Fetching: i18n-0.7.0.beta1.gem (100%)
Successfully installed i18n-0.7.0.beta1
Fetching: activesupport-4.1.6.gem (100%)
Successfully installed activesupport-4.1.6
Fetching: erubis-2.7.0.gem (100%)
Successfully installed erubis-2.7.0
Fetching: builder-3.2.2.gem (100%)
Successfully installed builder-3.2.2
Fetching: actionview-4.1.6.gem (100%)
Successfully installed actionview-4.1.6
Fetching: rack-1.5.2.gem (100%)
Successfully installed rack-1.5.2
Fetching: rack-test-0.6.2.gem (100%)
Successfully installed rack-test-0.6.2
Fetching: actionpack-4.1.6.gem (100%)
Successfully installed actionpack-4.1.6
Fetching: activemodel-4.1.6.gem (100%)
Successfully installed activemodel-4.1.6
Fetching: arel- (100%)
Successfully installed arel-
Fetching: activerecord-4.1.6.gem (100%)
Successfully installed activerecord-4.1.6
Fetching: mime-types-2.4.3.gem (100%)
Successfully installed mime-types-2.4.3
Fetching: mail-2.6.1.gem (100%)
Successfully installed mail-2.6.1
Fetching: actionmailer-4.1.6.gem (100%)
Successfully installed actionmailer-4.1.6
Fetching: thor-0.19.1.gem (100%)
Successfully installed thor-0.19.1
Fetching: railties-4.1.6.gem (100%)
Successfully installed railties-4.1.6
Fetching: bundler-1.7.4.gem (100%)
Successfully installed bundler-1.7.4
Fetching: sprockets-3.0.0.beta.2.gem (100%)
Successfully installed sprockets-3.0.0.beta.2
Fetching: sprockets-rails-2.2.0.gem (100%)
Successfully installed sprockets-rails-2.2.0
Fetching: rails-4.1.6.gem (100%)
Successfully installed rails-4.1.6
Parsing documentation for actionmailer-4.1.6
Installing ri documentation for actionmailer-4.1.6
Parsing documentation for actionpack-4.1.6
Installing ri documentation for actionpack-4.1.6
Parsing documentation for actionview-4.1.6
Installing ri documentation for actionview-4.1.6
Parsing documentation for activemodel-4.1.6
Installing ri documentation for activemodel-4.1.6
Parsing documentation for activerecord-4.1.6
Installing ri documentation for activerecord-4.1.6
Parsing documentation for activesupport-4.1.6
Installing ri documentation for activesupport-4.1.6
Parsing documentation for arel-
Installing ri documentation for arel-
Parsing documentation for builder-3.2.2
Installing ri documentation for builder-3.2.2
Parsing documentation for bundler-1.7.4
Installing ri documentation for bundler-1.7.4
Parsing documentation for erubis-2.7.0
Installing ri documentation for erubis-2.7.0
Parsing documentation for i18n-0.7.0.beta1
Installing ri documentation for i18n-0.7.0.beta1
Parsing documentation for mail-2.6.1
Installing ri documentation for mail-2.6.1
Parsing documentation for mime-types-2.4.3
Installing ri documentation for mime-types-2.4.3
Parsing documentation for minitest-5.4.2
Installing ri documentation for minitest-5.4.2
Parsing documentation for rack-1.5.2
Installing ri documentation for rack-1.5.2
Parsing documentation for rack-test-0.6.2
Installing ri documentation for rack-test-0.6.2
Parsing documentation for rails-4.1.6
Installing ri documentation for rails-4.1.6
Parsing documentation for railties-4.1.6
Installing ri documentation for railties-4.1.6
Parsing documentation for sprockets-3.0.0.beta.2
Installing ri documentation for sprockets-3.0.0.beta.2
Parsing documentation for sprockets-rails-2.2.0
Installing ri documentation for sprockets-rails-2.2.0
Parsing documentation for thor-0.19.1
Installing ri documentation for thor-0.19.1
Parsing documentation for thread_safe-0.3.4
Installing ri documentation for thread_safe-0.3.4
Parsing documentation for tzinfo-1.2.2
Installing ri documentation for tzinfo-1.2.2
Done installing documentation for actionmailer, actionpack, actionview, activemodel, activerecord, activesupport, arel, builder, bundler, erubis, i18n, mail, mime-types, minitest, rack, rack-test, rails, railties, sprockets, sprockets-rails, thor, thread_safe, tzinfo after 640 seconds
23 gems installed

Testing with arrays, the useful way

We’ve all seen this sometime when a test fails:

Failed assertion, no message given.

Now, I like using arrays in tests because they allow me to test a lot of cases with less code. A simple (and dummy) test could be:

However, when running this test, we will get the quite useless message above: Failed assertion, no message given. Right, we know the test failed, but we don’t know which case made it fail.

We can easily know with an often misused attribute of assert: message. As the docs say, we can pass a message together with an assertion, and it will be used in case of failure.

Now, when running this test, we know that the number 2 makes the test fail. It is so simple, but how often we forget!