July 25, 2007

Testing Emacs htmlize mode


def
receive_data data
      @tokenizer.extract(data).each do |stock_data|
        $data_log.info “———- #{stock_data}
        @stock_data = UbacParser.new stock_data
        if @stock_data.valid_code?
          dispatch_request
        else
          send_error_without_callback “Invalid Protocol code”
        end
      end
    end

April 24, 2007

A poem from the Kingdom of Nouns

Steve Yegge, had written a brilliant post, about Noun centric programming in Javaland. This poem takes the cake:


For the lack of a nail,
    throw new HorseshoeNailNotFoundException("no nails!");

For the lack of a horseshoe,
    EquestrianDoctor.getLocalInstance().getHorseDispatcher().shoot();

For the lack of a horse,
    RidersGuild.getRiderNotificationSubscriberList().getBroadcaster().run(
      new BroadcastMessage(StableFactory.getNullHorseInstance()));

For the lack of a rider,
    MessageDeliverySubsystem.getLogger().logDeliveryFailure(
      MessageFactory.getAbstractMessageInstance(
        new MessageMedium(MessageType.VERBAL),
        new MessageTransport(MessageTransportType.MOUNTED_RIDER),
        new MessageSessionDestination(BattleManager.getRoutingInfo(
                                        BattleLocation.NEAREST))),
      MessageFailureReasonCode.UNKNOWN_RIDER_FAILURE);

For the lack of a message,
    ((BattleNotificationSender)
      BattleResourceMediator.getMediatorInstance().getResource(
        BattleParticipant.PROXY_PARTICIPANT,
        BattleResource.BATTLE_NOTIFICATION_SENDER)).sendNotification(
          ((BattleNotificationBuilder)
            (BattleResourceMediator.getMediatorInstance().getResource(
            BattleOrganizer.getBattleParticipant(Battle.Participant.GOOD_GUYS),
            BattleResource.BATTLE_NOTIFICATION_BUILDER))).buildNotification(
              BattleOrganizer.getBattleState(BattleResult.BATTLE_LOST),
              BattleManager.getChainOfCommand().getCommandChainNotifier()));

For the lack of a battle,
    try {
        synchronized(BattleInformationRouterLock.getLockInstance()) {
          BattleInformationRouterLock.getLockInstance().wait();
        }
    } catch (InterruptedException ix) {
      if (BattleSessionManager.getBattleStatus(
           BattleResource.getLocalizedBattleResource(Locale.getDefault()),
           BattleContext.createContext(
             Kingdom.getMasterBattleCoordinatorInstance(
               new TweedleBeetlePuddlePaddleBattle()).populate(
                 RegionManager.getArmpitProvince(Armpit.LEFTMOST)))) ==
          BattleStatus.LOST) {
        if (LOGGER.isLoggable(Level.TOTALLY_SCREWED)) {
          LOGGER.logScrewage(BattleLogger.createBattleLogMessage(
            BattleStatusFormatter.format(BattleStatus.LOST_WAR,
                                         Locale.getDefault())));
        }
      }
    }

For the lack of a war,
    new ServiceExecutionJoinPoint(
      DistributedQueryAnalyzer.forwardQueryResult(
        NotificationSchemaManager.getAbstractSchemaMapper(
          new PublishSubscribeNotificationSchema()).getSchemaProxy().
            executePublishSubscribeQueryPlan(
              NotificationSchema.ALERT,
              new NotificationSchemaPriority(SchemaPriority.MAX_PRIORITY),
              new PublisherMessage(MessageFactory.getAbstractMessage(
                MessageType.WRITTEN,
                new MessageTransport(MessageTransportType.WOUNDED_SURVIVOR),
                new MessageSessionDestination(
                  DestinationManager.getNullDestinationForQueryPlan()))),
              DistributedWarMachine.getPartyRoleManager().getRegisteredParties(
                PartyRoleManager.PARTY_KING ||
                PartyRoleManager.PARTY_GENERAL ||
                PartyRoleManager.PARTY_AMBASSADOR)).getQueryResult(),
        PriorityMessageDispatcher.getPriorityDispatchInstance())).
      waitForService();

All for the lack of a horseshoe nail.

April 13, 2007

Using models in your migrations

When, you are coding data migrations, and decide to use model classes in your migrations. You are asking for trouble. Why?

Because, migrations is a fluid thing, and say you have decided to get rid of your model all together,then your migrations would magically start failing. This could be a problem.

Toolamantim, suggests an alternative. Simply redefine your models inside migrations itself:

#  class Product < ActiveRecord::Base; end
# class SoftwareProduct < Product; end
# class CourseProduct < Product; end
#
# def self.up
# add_column :products, :position, :integer
# Product.reset_column_information
#
# # Set default list orders
# SoftwareProduct.find(:all).inject(0) do |i,p|
# p.update_attribute(:position, i)
# i+1
# end
# CourseProduct.find(:all).inject(0) do |i,p|
# p.update_attribute(:position, i)
# i+1
# end
# end
#
# def self.down
# remove_column :products, :position
# end

Scaling rails

Scaling isn't easy, it never was. Its not easy with any dynamic language. So, twitter as a rails application is facing some issues of scaling. So while I was sleeping peacefully, a war was going on, in the community. Alex, the twitter developer shares his thoughts, http://www.radicalbehavior.com/5-question-interview-with-twitter-developer-alex-payne/ and DHH retorts back, and criticizes arms-crossed approach, http://www.loudthinking.com/arc/000608.html.

So, what shall a rails developer learn from this. Well, just the basics that - any fool can code web pages, its not rocket science for sure. But how will he react when problems like these strike him? LiveJournal invented memcached, Google invented GFS, Wikipedia does this by aggressive caching. You can't get a out of box, scaling solution. This is the precise reason, Why I have been adamant about hiring people, who know ruby better, who can dig into the internals and come up with shiny code.

Dr Nic, comes with a solution, http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/, which is not exactly the same. But lets see. Oh and dev.rubyonrails.org is a python page, which had same problem with trac.

April 11, 2007

require 'money'

From environment.rb:

 require 'money' 

Isn't it amusing?

March 27, 2007

CHM viewer in Linux

After trying out many crappy, CHM viewers in Linux, I have finally settled on kchmviewer.


A real nice app to use actually.

March 25, 2007

Law of nature

They have a factory that produces Singletons

life and guitar

As a kid, all I wanted to do is, play guitar and jump around, too many people got into my way.
—Syd Barret

March 05, 2007

Using class instance variable

We all know how evil class variables are, and they are as dangerous as the “three headed dog” at the dungeon and we shall not talk about it.


But they are necessary for many of the thingies ruby does and used extensively.But today we shall not talk about them.


We saw earlier that, although class instance variables are excellent, but not so friendly to use and they make Ruby look like C++(ahem).


Here goes a little hack, that allows you to define class level attributes based on class instance variables. Since, i often use it in rails and they have taken cattr for class level attributes.


class Object
def self.metaclass; class << self; self; end; end
def self.iattr_accessor *args
metaclass.instance_eval do
attr_accessor *args
end
args.each do |attr|
class_eval do
define_method(attr) do
self.class.send(attr)
end
end
end
end
end

What above code does is, creates those class instance variables at class scope and also creates accessor methods for it, so that it doesn’t have to suck when used from class instances(remember @foo.class.i_am_class_instance_var)


Here is a sample that, shows it in action:
class Foobar
iattr_accessor :foo
end
Foobar.foo = "Hemant"
p Foobar.foo
lol = Foobar.new
p lol.foo

Did i miss anything? comments?

March 02, 2007

just another cog in the wheel

We hate reinventing the wheel. But when the wheel doesn’t exist, or is square, we’re not afraid to invent a round one.