andhapp Random ramblings

*casts

I wonder did Ryan bates start a revolution by doing (RailsCasts) his short videos on various ruby concepts because there is a BDDCasts and today I saw GitCasts on Git’s main site. It is in the same fashion a collection of short videos on Git concepts but they are damn helpful, I tell you.

**Update: ** Found another site with screencasts on various ruby and rails concepts.

initialize method in ActiveRecord::Base

I spent quite a few hours trying to figure out why User.new (where user is a model) would not use the virtual attributes defined in the model class. Virtual attributes are the ones without a corresponding column in the table. Let me explain. I have a model called user which looks like this:

[gist id=201742]

Here, i_am_virtual is the virtual attribute. Let us say I have a form where I accept values for all the attribtues of the User model and that includes the virtual attribute (i_am_virtual) as well. Now, I was under the impression that when I do something like:

[gist id=201744]

it will automatically call the i_am_virtual= method so when I go looking for that value I can easily get it using:

[gist id=201745]

But unfortunately it does not seem to do it. I thought it was quite strange and when you have a doubt always refer the documentation but this time I went straight into the code. Here’s the code from ActiveRecord::Base’s initialize method:

[gist id=201727]

and the comments itself answers my questions. Really, I feel like an idiot now having spent all that time trying to figure out what was wrong with my code. But in the end it was a good exercise TBH.

Ruby 1.9

I was looking for the hash syntax in Ruby 1.9 as I could recollect that it has changed but did not remember it exactly and discovered this site that has all the changes in Ruby 1.9 in one place and in order. I know you can read “Programming Ruby 1.9” by Dave Thomas but then again this site would be extremely handy when you are upgrading your gems to Ruby 1.9. I wonder how many people have actually started using ruby 1.9. I do have it installed as a different binary (ruby19) but I have not been using it really.

rspec stub V should_receive

I have been pondering over the difference between the two aforesaid methods in rspec but it did not become apparent to me at first. Yesterday, whilst reading the RSpec Bible it all became crystal clear. I know some people might say “Oh…come on this is so easy” but I like to work these things out for myself because it is better in the longer run. Well let us have a look at the example from the RSpec book. Imagine you have a MessagesController and below is tiny bit of code from its spec:

before do 
   Message.stub(:new).and_return(message)
end

it "saves the message" do 
  message.should_receive(:save) 
  post :create
end

What is the intent of this method?

It is to test if the message is getting saved or not when a HTTP POST is performed.

Therefore, we concentrate on the “save” bit and any other calls which are part of that action(create in this example) and are required to achieve save, in this case, are just stubbed i.e. new. However, in the code snippet below we do not need to stub anything because we do not need to do call any methods prior to new:

it "creates a new message" do 
  Message.should_receive(:new).
  with("text" => "a quick brown fox").
  and_return(message)

  post :create, :message => { "text" => "a quick brown fox" }
end

Therefore, the spec methods basically ensure that one is testing just what the intent of the test is. Well…I could be absolutely wrong about all this but it is makes a bit more sense to me now. I will get it in the end hopefully.

* Please note that all the code sinppets are taken from Chapter 24, Rails Controller, The RSpec Book.

* Updated on 18.02.14 - Replaced broken gist links with code snippets.

I love metaprogramming

I have almost finished the 80% of ruby metaprogramming course and I am loving it. Really, writing a complete class without writing the code below. This is so old way of writing code…nah just kidding.

class People
end

Also, declaring the instance methods and using class_eval just obfuscates the code. Awesome. I will share the code here once I have finished it.