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.

Thursday 30 May 2013

LibreOffice is not showing CSV files properly

I get to deal with comma-separated(.csv) and tab-separated(.tsv) files from time to time and I have seen LibreOffice showing gibberish when I try to view the files.
The fix is actually very simple. I just had to set the encoding correctly.

Monday 27 May 2013

Separating data processor implementation and data processing logic

After about a year, yesterday I was looking some Mainframe source code. I was looking at some JCL to be precise. Looking at the familiar IDCAMS and REPRO procedures, I could get a sense of what the code was doing. Then I saw a sorting routine which used the program ICETOOL. I could figure out the input and output streams defined but I could not find the parameters for sorting. Finally, I figured they were getting passed through TOOLIN interface. Interestingly, the sorting parameters were kept in a separate file. This was nice programming: separating the rules from the implementation so that when the input changes, the changes to the rules can be made in that file only and the rest of the program works fine without any changes.

Thursday 23 May 2013

Search in AWS console

When I managed a small number of servers, looking up a server in AWS console was rather easy. However, as the number of servers grew, I started thinking of having a filter/search option. Fortunately AWS provides a nice search feature in the console.

Initial view when my instances were showing and there was no filter text.

After I added incorrect filter text, no matching instances were found.

Directory size listing

A lot of times when freeing up disk space [Well yes in today's world also people like me need to do it.], instead of opening my file manager and finding the size of the folders by checking the properties section in the right-click drop-down, I wanted to look at a list view where I can sort by folder size. Now, KDE's Dolphin file manager shows number of items within a folder but not the size and I anyways prefer the command line so I wanted a command line tool that lists size of directories. The amount of space left on the partitions can be easily found by the following command:

df -h

I also found a command for directory size.

du

However, its default behaviour is to show the size of files recursively. So, I do not get the size of the folders in the directory I am interested in. Instead, I get the size of files within those folders and recursively so forth. I needed to set the depth of search into the folders. I found a parameter with a similar name. The following command works just as I need.

du --max-depth=1 -h

Saturday 11 May 2013

Thursday 9 May 2013

Operating on each file in a directory in Windows Powershell

Recently, when I had to do some tasks on a Windows machine, I thought of trying out the Powershell. I had to process every file in a directory. In linux, I could easily do it on the command line so I tried to find out similar way for Windows. It turned out to be fairly simple actually.

$files=get-childitem .
foreach ($file in $files) { echo $file.fullname  }

Wednesday 8 May 2013

Sad state of medical profession

Today when a friend told me about an ad at olx.in about medical seats being sold (for those unaware of the concept, it means paying and getting into medical colleges irrespective of one's qualifications and intellectual merit), I was appalled. I knew that such things do happen. However, these deals were always secretly done. Openly selling seats in colleges is equivalent to committing theft in broad daylight and proudly announcing that it is the thief's right to do so. I believe it is blasphemy against humanity. It is a pity that people consider religious blasphemy very seriously but blasphemy against humanity is so simply ignored.

There is a propagation of crime going on here. These colleges bribe their way out of accreditation process or make a temporary show of fulfilling the requirements of the accreditation. Very rarely some are caught. Obviously the quality of education in such institutes is not up to the mark. The grave question then is whether we can trust diagnoses done by doctors passing our from such institutes that offer seats for sale. This actually is a more serious concern than global warming because incorrect diagnoses will cause undeserved deaths; but sadly no politician talks of it in his/her agenda.