Sunday, June 17, 2012

to_proc working magic

I had an interesting puzzle in ruby to capitalize words with in a string.
text = "this is an interesting blog post about to_proc" ' '

# "This Is An Interesting Blog Post About To_proc" 

Just was nailing down the answer I got, with the internals wired.
Normally single argument block comes after a map like this,
{ |arg| arg.capitalize! }

But how does ruby allows us to use syntactic sugar coated pill like passing a symbol to a map?
Explanation can be found here, symbol to_proc.
Need to smell some samples & create your own version of it? Here below...
class MyString < String

  def double
    self * 2

  def triple
    self * 3

  def map_char(&p)
    result = []
    self.each_char do |c| 
      result <<


name ="hariharan")

#usual way
p name.map_char { |m| m * 2} # "hhaarriihhaarraann" 

#syntactic sugar coated pill
p name.map_char(&:triple) # hhhaaarrriiihhhaaarrraaannn

On whole, the mist here is class 'Symbol' has a to_proc method, that accepts  object  as an argument and passes the message of 'symbol' to that object.

Stay tuned for my next blog about scala magic sugar coated stuff...

Saturday, February 4, 2012

Java 8 Lambda a terrible troll and Java is lame duck

            Nothing more to scare about the title, and more, this is not a post to slam or defamatory content against java. The Recent announcement about java 8 to support lambda and introduce functional programming abilities to java made me to wonder, really do we need this? Once my friend Mr.Balan said its Steve Jobs ,not apple and apple would had not been great without steve jobs. Its all about people who make things great rather than believing group or companies.

            Thanks to Mr. Cedric Beust whom  I respect a lot, his outstanding creation TestNG. The title of this post is the tweet he mentioned on me noting 'Terrible troll' when I asked 'Why google endorses java and why not something better than it'. Google employee tweeted saying that 'Google would had invented java if not exist'. More, google employees does not show overwhelming response to new JVM/CLR languages like scala and clojure. However Cedric can write some disappointing post like coding in high level type system languages would eclipse if not using eclipse from  scala back to java. This post made my adrenaline going. In fact prestigious, proud ruby developers use vim, yes honest authentic passionate ruby developers use vim, while some use textmate for ruby coding.

            Then I looked back at my poor java announcement that any chance for atleast java 8 to swing the theories of Functional programming. But in vain, where my respected oracle developers traveled with camels to thar desert, sat under khejri tree, intensively thought about building expensive lambda islands in par to what Dubai tries to. Unfortunately they failed like creating Indian Mumbai building constructions with poor quality material. In 2002 C# finished chapter, Scala moreover finished in providing lambda in 2005. Then, what is the point in creating a huge buzz about lambda in java 8 by the year 2012, then saying 'Java will adopt C# syntax'? This should have been wisely copied or taken 8 years back.

            Java already screwed with some concepts like anonymous inner classes. Though I am not object oriented or based specialist, but really I could not connect with reality about class nesting inside a class. Can a person nest a  person inside? If so what is the use of that.

            Actually speaking, this is what happens with sorting a collection, where comparable interface is implemented and collection.sort in turn calls the comparaTo method for status, to swap the element positions. But where is the thrill?

            Java 8 lambda is a lame duck. Because there are no first class functions. Functions are not senior citizens. Otherwords, its more or less same way of achieving to sort a collection by implementing comparable interface. Whereas in scala, functions are senior citizens. Let me get straight to examples.

Open scala prompt, to find a minimum element in an list,

val values = List(1, 2, 3, 4, 5)
println(values.foldLeft(0) { (acc_res: Int, index: Int) => if(index < acc_res) index else acc_res }) 
//prints 0 //To find maximum, (just change the comparison symbol :)) def maxi(acc_res: Int, index: Int) = { if(index > acc_res) index else acc_res } println(values.foldLeft(0) { maxi }) //prints 5

What the same to do in Java, to find the minimum element, comparable interface must be implemented or custom interface is created in favorite naming in favorite eclipse, that hangs windows xp for 5 mins, a method declared in interface, and then defined to return status in implementation class. Finally call made to implementation class that implements of type 'comparable' sort of interface(or custom interface)

Now coming to Java 8 Lambda, serious comedy fuels, just look at the sample lambda program

public class SimpleLambda {
  interface HelloWorld {
    void greet();

    HelloWorld greeting = () -> System.out.println("Hello World!");

  public static void main(String... args) {
    new SimpleLambda();


Type of anonymous functions or lambda? Its the same as interface.
Hence interface should be defined prior to assigning a anonymous function which is not the intent of lambda in other languages.

To add, a question already in heavy discussion on quora for oracle to declare explicit support to scala instead of spreading fad.

Since Java ones are tamarind to declarative programming, scala itself more sufficient.

Microsoft .NET can better declare explicit support for clojure, as I believe C++ and C# programmers be intuitive and pick up clojure on CLR.

James gosling intent of java is just to please the average coders, I am not one and you too.,,

Enjoy functional programming, DSL and kudos to Scala and Clojure.

Sunday, November 13, 2011

Steve Jobs steamed well

Being technology hypothetical and haunting web, I could see people honoring and paying tribute to some one who is no longer alive, to happy the conservatory ones(Remember Swiss bank) who tried to meta retweet, meta liked posts in facebook to let it know that it can relevantly push some Apple store Ads and halted at one point thinking no longer these buffaloes can be milked, as hands started paining. But if you think whom I am honoring here, i am delegating that to decide at your discretion.

Steve Jobs, founder and CEO of Apple, who died recently pioneered the personal computer, though microsoft took over business in PC era. Quite dissatisfied by the Apple board directors, where his products intended to create for educational sectors didn't went well, forced Jobs to quite and start his new company Next. His new founded company developed a wide range of software and hardware. Company uprooted when Jobs could succeed by developing object oriented OS 'NextStep', that uses a hybrid kernel(monolithic of XNU and microkernel of Mach) and using Objective C, which took BSD(a Unix variant), modified at architecture level.

This made Apple to realize and call back Jobs by acquiring his company 'Next'. Jobs return made Apple to raise its eyebrows by his stunning leadership qualities, where he is good at driving ideas and innovation, could figure out exactly what people needed and see implementation. One cannot forget the man behind iPod, iPhone, iPad(what next would be? iP*****;)) that took company to heights, revealing his product creativity genius. Jobs suffered from a rare form of pancreatic cancer, a terrible disease but he could sustain and extend his life span bit more due to latest medical innovations happening in that stream.

One of his greatest strength - leadership being hostile to his employees by stripping down many unwanted products and conflicting ideas, which might have taken a stroll on Apples growth. Despite large insignificant number of mentions in social media streams to my dismay, his coveted, elite products went well with channeled marketing strategy. His notable incident include when ipod design was first rejected, not appealing his sensations when employee took the prototype to him. When employees remained unclear about the rejection, he put it on a water tank and notified them of water bubbles that occupied empty space, reaching bottom.

On large, Dennis Ritchie was the man indirectly behind his(and many other) success, who created C language and Unix OS(which Steve Jobs took off the base for MacOS), where he also passed away following Jobs to super world, without knowing why he got less number of mentions. Without him, we would have not seen Windows, Linux, Minix, Mac and C++, C# and other languages. Hmm, you might say that's what applicative business mind is all about!.

Jobs being identified on his own strength, nothing makes me to flirt, one - MacOS requiring extensive, expensive Hardware to run due to the inappropriate design, second - Objective C being informal OO language and irritating, clumsy development, third - not strategizing MacRuby and pioneering it as a mainstream Apple language, fourth - most important Apple could make products only for hi-fi business magnets who will be ready to spend money lavish, not for common ones. Though Jobs at small corner thumping minimal at my heart, I am deeply saddened by the death of Dennis Ritchie and how come can we forget G.D.Naidu and his contributions?

P.S - It is also to be noted MacOS being greatly affected by virus more than windows, though systems lesser in number

Wednesday, August 17, 2011

Ruby GUI and FXRuby Installation

Why about GUI and FXRuby all of a sudden?

Its a tremendous movement we made from desktop to web 2.0 apps and again back to native desktop apps(iphone/ipad) Seesmic/Tweetdeck apps broadly classifying as RIA.

Hence my long time dream, has been pondering about throwing the silver cup of Java swing and bringing platinum Ruby stones in GUI front, finding space in replacing existing legacy Java swing applications with a neat Ruby GUI like TK-Ruby.

To my horror, nothing has been honored in Ruby world apart from Rails and geeks see Ruby as a Web DSL and where as rails has made routes.rb itself a DSL.

I forayed to figure out any Ruby GUI library in par with AIR/Silverlight, but my search collaboration with mountain view only ended up in disparity.

Finally ended up in installing FXRuby to see fortune in it, written a small GUI ruby program as below,

require 'rubygems'
require 'fox16'
include Fox

application ="CompositeGUI", "CompositeGUI")
main_window =, "Composite",nil, nil, DECOR_ALL)
main_window.width = 400
main_window.height = 200

#Vertical Frame
super_frame =,LAYOUT_FILL_X|LAYOUT_FILL_Y), "Text Editor Application")

#Horizontal Frame
text.text = "This is some text."

# Button bar along the bottom
button_frame =,LAYOUT_SIDE_RIGHT|LAYOUT_FILL_Y), "Cut"), "Copy"), "Paste")

#create FX application and run

and when I ran, landed with the below error message,

LoadError: no such file to load -- from line no 2 require 'fox16'
I installed fxruby-1.6.20-x86-linux.gem and wondering any fox library kit need to be installed in my system and searched in my ubuntu for fox library dependency but left with no clue.
Then I uninstalled and downloaded fxruby-1.6.20.gem and installed. Native extensions were built, then I could ran my fxruby program with ease,

Though not appealing, enough qualified to place it beside AIR or Silverlight GUI, this could be the half brother of Java Swing.

For enterprise Java swing applications and migrating it ruby I saw this article, 
Don't know practically how far this works

Tuesday, June 21, 2011

Ruby Modules: A perfect name space resolver?

Coming from java background, I was looking for package equivalent in Ruby and read in Programming Ruby 1.9 book that Ruby Module serves two purposes, 1) Namespace conflict resolver 2) Mixin Though, mixin is applauded one, now the one that bothers me is Namespace conflict resolver. lemme idealize it in to a simple example
module A

def test
  puts "I am in module A"


module B

def test
  puts "I am in module B"


Class Payment

include A
include B


p =

p.test #prints - I am in module A

Now how to invoke test method in module B? But the same if I rewrite the those module methods as a class level methods, this works
module A

def self.test
  puts "I am in module A"


module B

def self.test
  puts "I am in module B"


Class Payment

include A
include B

def test_modules


p =

p.test_modules #prints fine - I am in module A, I am in module B

when I do,
p.A.test #bangs - `
': undefined method `A' for # (NoMethodError)
What is this actually? I couldn't understand the concept, Is someone who could help me on this? Is Ruby a perfect namespace conflict resolver?

P.S - I got to know clear about this by watching this video MetaProgramming for Fun by Dave Thomas

Wednesday, March 2, 2011

Ruby Floating floats like a Dead Fish

I was so serious in attempting to solve a problem that Thoughtworks usually gives to its new recruits to test their Object Orientation skills. In that there is a Sales Tax problem which involves lot of math library, floating point calculations rather than using our brain memory in it.

While when I do the rounding of a sales tax calculation to the nearest 0.05, I faced the problem in my ruby
Though I agree that this is my first time where I am doing intense floating point calculations and round off, could someone explain me why this inconsistency happens with ruby?

I need at least two decimal places right in my calculations, rather than implementing a work around to round off a 8 decimal float number .

Also it should be noted that round(n) as written in Float class in ruby, which rounds to 'n' decimal places doesn't work with Jruby. Any clue on this???

Sunday, December 26, 2010

How to run PHP files inside public_html directory in Ubuntu 10.10 a.k.a Maverick Meerkat

By default, php files doesnt gets interpreted when placed inside public_html in Ubuntu 10.10. ie in Maverick Meerkat.

When you install php5 in ubuntu 10.10 or 10.04, planning to place your php files inside public_html directory in your user area, here is the solution for you.

Create public_html in your user area

First of all check whether enable-modules under your apache server
for that
goto /etc/apache2/mods-enabled/ folder and check userdir.conf, userdir.load modules are enabled by simply typing ls command

if not then goto /etc/apache2/mods-enabled and create symbolic link as follow

sudo ln -s ../mods-available/userdir.load

sudo ln -s ../mods-available/userdir.conf

or you can enable using the following command

sudo a2enmod userdir

Finally create public_html folder under your home directory an give full access permission to it.

Restart your apache server (sudo /etc/init.d/apache2 restart or service apache2 force-reload)

Access it through following link.

http://localhost/~[your_home_directory_name]/ (for me it's http://localhost/~hariharank12/)

To run PHP files inside public_html in Ubuntu 10.10

In the new php.conf file which is present in /etc/apache2/mods-available/php5.conf, the php engine inside user directory is turned off as like below.

 php_admin_value engine Off

Comment those above lines.

Finally php5.conf will look like below.

        SetHandler application/x-httpd-php
        SetHandler application/x-httpd-php-source
    # To re-enable php in user directories comment the following lines
    # (from  to .) Do NOT set it to On as it
    # prevents .htaccess files from disabling it.
#            php_admin_value engine Off

Again restart the apache web server.

Test with some php files placing inside public_html folder and hitting URL with localhost/~username/php_file_name.

It should work now! :)

For more detailed description why php files running inside public_html is disabled, see the following link