Favorite languages, why so great? and why not so much?

About my favorite languages, I actually have 2 favorite languages

  • Ruby: for all scripting and making quick apps.
  • Clojure: for development.

Why Ruby is great

  1. The language was designed for programmer use, you can see that from the api which is totally intuitive.
  2. Lots of libraries, my favorite is Sinatra which lets you build quick and dirty web apps and the other is Sequel.
  3. I wrote a blog post on how to delete RFC-822 in compatible emails (if you are a developer using linux and your company uses Outlook you know what I am talking about), this is a simple example of how I have used Ruby to make quick and dirty scripts.

I have used Ruby numerous times to write scripts to fix production data, correct files, and to generate complex reports. I have used Sinatra with Google Charts to make web apps that can show load times, server status ….

Why Ruby is not so great

  1. Not really meant for performance, recent years there is a push to develop a virtual machine for Ruby but it is still not anywhere close to C/Java performance.
  2. Rails is a pain to deploy, Heroku takes away the pain but what do you do if you have to deploy internally ? I personally have 2 apps on Heroku one of which is http://first3links.com/

Why Clojure is great
I have been on a quest to learn a functional programming language for the past 3 years, I have read the Erlang book (please see the various posts I wrote about Erlang here). Erlang is a fine language but I lost interest in it after I could not find a single good library that can connect Erlang to Oracle. The problem, there are too few 3rd party libraries. The next language I looked at was Haskell, lots of libraries and seems to be good at performance on the surface, problem I see is acceptance by business, where most of the code is in Java. Then I found Clojure and fell in love with it.

  1. It is just another DSL for the JVM, if you provide type hints the code generated will be the same as what Java would (can easily sneak it in).
  2. Totally embraces the JVM unlike JRuby.
  3. The author Rich Hickey has done a lot to reduce the pain points of lisp.
  4. Finally a language that frees you mind of OOP ( Have you ever noticed how much time you spend in trying to achieve the best object model when a simple one would do ? and for what ? the customers don’t care as long as it works, the computers sure don’t care as long it is 0s and 1s)
  5. Code is so concise and elegant.

Why Clojure is not great.

  1. It has been called as the language with the steepest learning curve on the JVM, I tend to agree with it.
  2. Unlike Scala you have no wiggle room, it is either functional code or nothing ( I like this feature actually).
  3. Debugging is a major pain point. (Though there has been improvement with the latest clojure-swank).

I have written many posts on Clojure on my blog you can see them here. In the most recent post I show you one can parse a one million record file in less than 15 seconds with clojure.

Uploading and Serving files with Amazon S3/CloudFront and Rails

The attachment_fu plugin written by technoweenie takes care of uploading and retrieving files using S3 and CloudFront, it has other options too such has saving to the database, local file system, rack space. Anyways in this post I describe how to use the plugin with S3 and CloudFront

  1. Create a sample rails project.
  2. Install aws-s3 gem, ‘sudo gem install aws-s3‘.
  3. Install attachment_fu plugin “./script/plugin install git://github.com/technoweenie/attachment_fu.git“.
  4. Assuming that you already have an Amazon S3 account, create a new S3 bucket.
  5. Next step is to sign up for the Amazon CloudFront, this is Amazon’s CDN and like any other AWS you pay for what you use, in addition it integrates with your S3 buckets.
  6. Now log into the CloudFront’s console and create a new distribution channel that is backed with the S3 bucket created in above steps.
  7. Rename amazon_s3.yml.tpl to amazon_s3.yml (it will be in your config folder)
  8. Edit the amazon_s3.yml file and fill in the appropriate information.
  9. Make a model ‘./script/generate model file_meta_data size:integer content_type:string filename:string’ .
  10. Edit the newly created model and add the following lines to it.( ‘has_attachment’ and ‘validated_as_attachment’ are provided by the plugin, there are many other options that you can specify to read more on the options refer to this page).
  11. has_attachment   :storage => :s3,
    :cloudfront => true
    validates_as_attachment
    
  12. Generate controller ‘./script/generate controller Upload index show new edit create update destroy’.
  13. Edit the Upload controller and add the following
  14. def index
      @fileMetaDatas = FileMetaData.all
    end
    def new
      @fileMetaData = FileMetaData.new
    end
    def create
      @fileMetaData = FileMetaData.new(params[:fileMetaData])
      if @fileMetaData.save
        flash[:notice] = 'File was successfully created.'
        redirect_to :controller => :upload, :action => :index
      else
        render :action => :new
      end
    end
    
  15. Edit the new.erb.html file under the upload controller folder and add the following
  16. <form_for(:fileMetaData, :url => upload_file_path, :html => { :multipart => true }) do |f| >
    
    Upload A File:
    <%= f.file_field :uploaded_data %>
    <%= submit_tag 'Create' %>
    <% end -%>
    
  17. Edit the index.html.erb file under the Uploads controller and add the following
  18. File List
    <% for fileMetaData in @fileMetaDatas -%>
    <%= link_to fileMetaData.public_filename,fileMetaData.public_filename %>
    <% end %>
    
    
  19. Edit the Routes and add this new route “map.upload_file ‘/new’, :controller => ‘upload’, :action => ‘create’
  20. Run migrations
  21. And run the server, that’s it now you browse to http://localhost:3000/upload/new to upload a file.

Integrating Facebook Connect into your Web App

Lately I have been spending time researching on how to integrate with facebook connect. There is a ton of documentation on the facebook wiki page, but as with any wiki you need to know what you are looking for. I am going to document here what I found.

For the really impatient (like me) check out this video.

  1. To begin with go to this Wiki page on facebook to get familiar with facebook connect and reasons for using it.
  2. Install the facebook developer app into your facebook page (makes it easy to keep track of your api keys and your webpages ).
  3. Next on the facebook developer app register your domain and it will generate  the api key and secret (don’t worry about saving it you can always look it up from the developer app).
  4. Once you have done that you will need to download and place this file xd_receiver.htm into your applications root folder. (For step by step instructions to do this go here.)
  5. Include this tag in every page that you plan to use facebook connect on.
    <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>    
    
  6. And also include this snippet to initiate the connection.
    FB.init("your key", "/xd_receiver.htm")

    Should be in the body not head

  7. Place the login button
    fb:login-button length=’long’ onlogin="successful_login();"
  8. You need to create the function successful_login, that will do something when facebook authenticates the user. I just went and pulled the user’s picture and show it. The code for that is here
    $("#login").html("<fb:profile-pic uid='loggedinuser' facebook-logo='true' > </fb:profile-pic>. Welcome,  <fb:name uid='loggedinuser' useyou='false'></fb:name >.");

And that’s about it, if you want you can check out my example at devender.net