Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Rails: Non-DB field in Model with validation

leave a comment »

Challenge: Add a field to the model representing system date, time & timezone that is not stored in the database and have validation on it.


  • Add attr_accessor & attr_accessible (for bulk update), add getter (sys_date_time) & setter (sys_date_time=) methods.
  • Return system date time in UTC from getter and show in user (browser) timezone on client side (using moment-timezone.js which is moment.js extension select the download with timezone data)
  • When the setting come to be saved using update_attributes in controller it eventually calls model setter (sys_date_time=) where you convert it back to utc given the timezone info (ActiveSupport::Timezone[zone] is your friend with ActiveSupport::TimeZone[zone].parse then in_time_zone(‘UTC’))
  • Validation is tricky as this is not a db backed and getter returns the calculated value which is always valid. You need to validate input first thing in setter and set a new member variable with errors found. Then add a validate do … end block where you iterate through the new member variable with errors and do errors.add(attr, message).

Will add code snippets when time permits.



Written by imsaar

April 7, 2016 at 2:02 pm

Posted in code, rails, ruby

Siri Sucks! Worst User Experience of Any Voice Based Assistant

leave a comment »

Siri is the worst implementation of voice based assistant today. Apple has done very little to improve it overtime so far. I have been using iPhone for many generations of iPhone now and am constantly disgusted and frustrated by Siri.

In contrast, whenever I have used Google’s implementation of voice based assistant either on iPhone or somebody else’s Android it has never let me down. Google’s implementation is far superior. One of the ways it is superior is it does not need an active internet connection to dial a number from my address book or many other local tasks.

Being a software developer I cannot understand why Siri cannot do anything without internet connection. There is no need to send everything to the cloud with a potent device like iPhone in hand. I remember my early MS Window Mobile (circa 2006) could do much better voice recognition than Siri.

I do not understand why it is hard to voice dial people in my contacts or detect their name also why there is no feature to announce who is calling on an incoming call.

Bottom line Siri sucks and if you are using iPhone you are stuck with it. If Apple can’t do a good job improving Siri at least they should let you plugin your own voice assistant as a first class provider (system wide not just in a particular app).

Written by imsaar

November 2, 2015 at 2:03 pm

Posted in general

My Advice To New Software Developers

leave a comment »

  • Don’t settle too early and don’t artificially constraint yourself. For instance:
    • Don’t say I am a Java or .NET developer and restrict yourself to only those platforms
    • Don’t stay in the same company for too long, some times you settle for 1 year of experience repeated 5 times rather than 5 years of real experience. You can always come back to a company if you like it better than the next one. In large companies you can move to a different group.
    • Don’t move too fast, take the time to learn from the smart people around you. If you are the smartest person there leave immediately.
  • Keep learning new and perspective changing technologies and not learn more of the same. For instance:
    • If you know a object oriented language (C++) learn functional language like Haskell
    • If you know a statically typed language (Java) learn a dynamically typed language like Ruby
    • Keep an eye on the trending technologies and briefly explore them to get a taste of them
  • Teach what you have learned to others, you have learned it if you can teach it

Written by imsaar

September 3, 2014 at 12:37 pm

Ruby: TPK Algorithm in Different Programming Languages

leave a comment »

I was looking for some exercise problems for a programming class I am teaching and encountered Knuth’s TPK problem on comparing solutions in different languages but did not find my favorite language and thought I would do it in Ruby and may be submit for inclusion there.

The TPK algorithm reads in an array of 11 values, applies a particular function to each value, and then writes the result in reverse order. It serves just to illustrate some of the usual actions that an imperative programming language must perform.

Here is my quick solution:

inputs = []

def f(x)
  Math.sqrt(x.abs) + (5 * x ** 3)

11.times do
  inputs << gets.chomp.to_f

inputs.reverse.each_with_index do |num, index|
  fn = f(num)
  if fn < 400
    puts "#{10 - index} #{fn}"
    puts "#{10 - index} #{fn} TOO LARGE"

I think it could be done better but just wanted to have something out there.

Written by imsaar

August 28, 2013 at 2:33 pm

Posted in ruby

Vim: Using Ruby Regular Expression

leave a comment »

I needed to remove line number from a web copy-pasted code snippet. I know there is a way to configure vim to use the perl style regular expressions but I couldn’t find how quickly.

What I found was:

:rubydo gsub /pattern/,'replacement'</code>

Here is my specific command:

:rubydo gsub /^\s+\d{0,3}\s/, ''

Pre-requisite: look for +ruby in :ver output (if it is -ruby then it would not work).

The above web page also talks about Perl regular expression in Vim and different ways to do that.

Written by imsaar

August 28, 2013 at 2:17 pm

Posted in ruby, vim

How I got Flash working on Kindle Fire HD 8.9″?

with 11 comments

The Amazon Customer Experience was not very eloquent about flash being not supported anymore and wasted my time.

I ended up on some good Amazon customer forums entries that helped me, here is the best forum entry I found.

Here are the steps to get Flash working on your Kindle Fire HD 8.9″:

  1. Download ES File Explorer from Amazon App Store.
  2. Download Dolphin HD apk from your Silk Browser.
  3. Download Adobe Flash Plugin from your Silk Browser
  4. Open ES File Explorer app and go to Download folder.
  5. Open the Dolphin HD apk file. You will get a prompt to about Application Install Blocked.
  6. Press the Settings button and Allow Installation of Application from unknown sources.
  7. Now hit the back button (or go back to ES File Explorer and click on Dolphin HD apk again).
  8. Next click on Adobe Flash plugin also and open it using Dolphin HD browser and finish installing Flash.
  9. Open Dolphin HD browser app and open your favorite flash website.

Written by imsaar

November 21, 2012 at 6:55 pm

Posted in general

Tagged with , , , , , ,

Kindle Fire HD 8.9″ Experience

leave a comment »


Great affordable price.

Amazon Free Time feature for have a sandbox account for children with set allowed apps, books and daily time limits. This is a main reason to get Kindle Fire HD for me (I have iphone and ipad and can act as an Apple fanboy at times).

Integration of all the Amazon Services in one device. Audible, Books, Apps etc.

Amazon App Store let’s you know if this app is works for your Android device. A major improvement on what I have seen for other Android Tablets



The power and volume buttons are very hard to press. They are pretty much flushed with the body and hard to find at times without looking hard for them.

Customer Service is not educated properly on all the features, specially Immersive Reading Experience. If you have an issue with that please contact Audible Support.

Flash does not work and is not supported anymore but not their fault as Adobe has stopped supporting it. I finally found a way to get Flash working on Kindle Fire HD 8.9″

Immersive Reading Experience can very frustrating as I downloaded both the Book and Narration and still did not work for me for some books. Even working with customer support for hours did not resolve it.


Written by imsaar

November 21, 2012 at 6:41 pm

Posted in general

Tagged with , , , , ,