Jun 042012
 

This would serve as a quick reference for HAML.

HAML is a markup language that gets transformed into HTML. you may ask, why not write write my code in HTML, What’s wrong with HTML? And how does HAML solve does the problem?HAML_logo

Problem with HTML

HTML is verbose & becomes hard to maintain specially, if you have a large site. To see why HTML’s verbosity is issue, consider this example:

HTML

</pre>
<div id="profile">
<div class="left column">
<div id="date"><strong>4th June 2012</strong></div>
<div id="address"><em>Jaipur, India</em></div>
</div>
<div class="right column">
<div id="email"><a href="www.example.com">example.com </a></div>
<div id="bio">Rails Developer</div>
</div>
</div>
<pre>

As you may notice, there are couple of issues with html:

  1. Every starting tag has to have a ending tag
  2. The number of words & lines are more
  3. plain HTML is pain to write & read

Now, compare this to HAML which basically does the same thing but in lot more human readable form

#profile
.left.column
#date
       %strong 4th June 2012
#address
       %em Jaipur, India
.right.column
#email
       %a{:href="www.example.com"}
            example.com
#bio   Rails Developer

So in short,

Syntax Description
%tag_name produces opening & closing tags
%tag_name.class_name add a class attribute to given tag
%tag_name#id_name adds a id attribute to given tag
- runs the native code(ruby etc) but doesn’t not display result
= runs the native code(ruby etc) and display output
#body Produces div tag with id=”body” as div tag is default

For more, kindly refer to HAML official site

May 192012
 

I dislike java, by dislike I mean I do not enjoy programming in java but I still use it in situations where java feels best tool for the job, because I am technology agnostic & belive in using best tool for the job; for example writing Android Apps. And I admit I enjoy writing ruby code that much more than java that I will even write ruby from my deathbed. sounds crazy! I know!. But it’s not fanboyism, because for writing webapps — ruby & the rails stack is TEH best. In future, if something better comes along, I would switch in a heartbeat because I really believe in using best possible tools to get the job done

Now, let’s go back to the reason of this blog post. Why do I like ruby more:
well, there are lot of reasons but for me, the main reason is the philosophy behind the langauge

Ruby is designed to make programmers happy

- Matz, Creator of Ruby

To explain this, consider this ruby code:

5.times {print "My Name is Gaurish "}

so even if you do not know ruby programming or heck do not know programming at all, I am pretty sure you can know what this code is doing does just by reading aloud words by word. Why, because it reads like “5 times print my name is gaurish”.

Lets take another example, this time a bit more complex , to read a file & then print it.  here is the code to do that in Java

class FileOperations{
static void readfile(String filename) throws FileNotFoundException {
  File input = new File(filename);
  String line;
  Scanner reader = null;
  try {
    reader = new Scanner(input);
    while(reader.hasNextLine()) {
      System.out.println(reader.nextLine());
    }
  } finally { reader.close(); }
}
public static void main(String args[]){
 readfile("hello.txt");
}

}

now, compare this to ruby code which does the same thing without using classes which is mandatory in java.  Another thing is, the Java version needs to wrap the creation of the Scanner in a try block so it can be guaranteed to be closed.Now, lets compare this to ruby version of same program

def readfile(filename)
  File.open(filename, "r") do |f|
    f.each_line {|line| print line }
  end
end
readfile "hello.txt"

Because of the existence of blocks, it is possible to abstract away the need to close the File in a single location, minimizing programmer error and reducing duplication. And this is one of the reason, I like ruby more than Java. There are plenty of other reasons & maybe I will cover those in future posts, if anyone is interested, then leave a comment below

PS: I have used print, instead of puts because to highlight the readability of ruby. But if you write ruby more, you will find yourself using puts as it auto inserts a new line which has to be manually inserted when using print.

Apr 232012
 

Ruby 1.9 has a cool hash syntax which is quite similar to javascript, so the following hash

hash = {:symbol => "value"}

can be written as

hash = {symbol: "value"}

now, what if both key & values are symbols, in ruby 1.8 & before you would write

:pick => :any

but now, this could be written in short form as

pick: :any

And this both new & old syntax for hash are supported in ruby 1.9, you can use whatever syntax you are comfortable the most but it helps to know both forms of syntax specially when reading the code that other might have written. personally, I like the new one — fewer characters to type :P

 

PS: this new has syntax is applicable only if the key is a :symbol. In case when key is not a symbol. you have to still stick with old array syntax(=>)

Apr 142012
 

Heroku is the popular ruby on rails PAAS hosting platform. With the earlier versions, if you were hosting a rails web application on it. All apps deployed to Heroku used to automatically compress pages they serve, by passing through Nginx’s gzip filter on the way out. But with their newest Cedar Stack, things have changed

In Cedar, the HTTP requests terminates directly at your app server & no longer goes through a proxy server(Nginx), hence there can’t be automatic gzip compression. More information about this can be found in HTTP routing dev center article So we have to manage gzip compression on our own. fortunately, this is trivial as adding just one line to your config. basically, you will need to use Rack::Deflater & make sure that it get loaded before ActionDispatch::Static. The simplest way to enable gzip compression in Rails 3.2 is by adding “use Rack::Deflater” in config.ru file.

After the change, this how your config.ru file should look like:

# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run Improvingoutcomes::Application

I spending sometime gather information about this. So Hope this post help you save some time.

Apr 082012
 

I shared my views on GPS technology by a journalist. As everyone knows, I love to share my views on technology. Like little did I knew that it would end up in Major National Newspaper of Country – Times of India.

This article made into Times of India on march 11 2012, I got little delayed in posting it here. Not something big but it feels nice to read your name in paper upon waking up :)

you read the article Online at:
How your phone can be your compass – Times of India

Feb 032012
 

Caching helps to cut the load on server & increase server speed. Hence, Drupal comes with caching build-in at various levels. there will be situations where,  you may want to disable *ALL* caching like I was doing some module development and without clearing out the caches the changes were not visible. So, I had to disable caches.  Incase, you too want to disable Caching for say for development – here below

  1. Go to Site Configuration -> Performance:
    • Set the following options, and click Save configuration:
      • Caching mode: Disabled
      • Minimum cache lifetime: none
      • Page compression: Disabled
      • Block cache: Disabled
      • Optimize CSS files: Disabled
      • Optimize JavaScript files: Disabled
    • Click Clear cached data.
  2. Go to Site building -> Views -> Tools:
    • Check Disable views data caching and click Save configuration.
    • Click Clear Views’ cache.
  3. Install the Devel module, and go to Site Configuration -> Devel settings:
    • Check Rebuild the theme registry on every page load and click Save configuration.
  4. Add the following to your .module file to disable menu caching

function hook_init(){
   //FIXME: remove before going into production
   menu_rebuild();
}

With this, now ALL caching in drupal will disabled. Enjoy quicker development & don’t forget to undo these changes once your site goes into production!