Showing posts with label Intuition. Show all posts
Showing posts with label Intuition. Show all posts

Friday, 31 May 2013

[Rails:] false.present? being false is not intuitive (to me at least)

In Rails I often use the present? method to check whether a key-value pair is present is an input hash. The code would be something like the following.

def trial_method(params)
    o = Model.where(:id => params[:id].to_i).first
    o.field = params[:field_key] if params[:field_key].present?
    o.save!
end

This works fine when field is most cases. However, when the field is a boolean field, there is a subtle with this code.

When params[:field_key] is true, the code works fine; but whenever it is false, the code fails. The intent is to see if the key-value pair is present and if present the value should be assigned to the field. Now, the value can be both true and false and both of those should be assigned to field. However, when the value is false, the present? function returns false and the field is not set any more. So the field value stays true and with the above code can not be flipped to false. The correction is of course simple.

o.field = params[:field_key] unless params[:field_key].nil?

This behaviour did not seem intuitive to me. So, I looked up documentation and source for present?. The definition of present? is quite simple.

def present?
  !blank?
end

Clearly all it does is to negate the result of blank?. So, I looked up the definition for that too.

def blank?
  respond_to?(:empty?) ? empty? : !self
end


When I tested false.respond_to?(:empty?), it returned false. So, it is clearly executing the !self part, which translates to false.blank? being true and therefore false.present? being false.

Saturday, 23 May 2009

The changing web

We all know the web is changing and along with it our lives as well (though the extent and kind of change is determined by the our own decisions). While surfing I found this. I was impressed to see the way it responded to mouse gestures. The design is intuitive. Developments like these are welcome. They make proper use of the power of current technologies. I could see Google's philosophy playing a role. The interface was not coming in the way.

Friday, 27 February 2009

Saturday, 26 January 2008

Microsoft Surface

Microsoft's futuristic computer is called Microsoft Surface. Earlier we used to install computers as desktops but they plan to turn desktops into computers. There are no keyboards or mice. It uses IR cameras to detect touch.
This is going to change the whole experience of desktops.

The implications vary wonderfully for common public and developers. Computer programmers will need to develop more complex algorithms. The hardware will also be more sophisticated. The input and output options will increase and thus their respective modes will diversify. This diversification will make the computermore interactive for the user. It will be easier, simpler and friendlier. Computer games will be a much more multimedia-rich experience.
No swiping of would be required for systems running Surface. You just need to throw your card on the surface and the system takes care of the rest. You just move your hands to signal the system.

Wednesday, 23 January 2008

Beyond intuition

Intuition guides many of our decisions and often we make use of it subconsciously. Sometimes a strong sense of intuition is also referred emphatically as "sixth sense". However, nature isn't always intuitive. Prof. Walter Lewin demonstrates the same in the following video.