Lyrical Whisprings#3 : The BOC perspective

I mean and I really mean, these guys are brilliant. I can’t really say, what I like in their music, but all their tunes sound oddly familiar and coming from a distance, like a bazooka from past. I love it, I wish I could do that.

Terrifying thing is, they are not afraid to release their albums in numbers like 100 or even 5. Now, how the hell, a poor guy sitting in India would ever get his hands on something like that. I am yet to see any of their records on a music store shelf.

Here Marcus talks about artists who are too fucking big:

“No. We think they’re brilliant,” Michael demurs. “I think Kid A’s the best thing they’ve ever done,” adds Marcus in his thicker Scots slur.
“Artists whose status is somewhere between Radiohead and God,” answers Marcus, mystifyingly

Above quote is the context, when Marcus criticizes mainstream musicians or rather musicians who make
music based on pop culture. The interviewer prompts,

“Who are these Bigger Guys, Radiohead?”

Needless to say I hate this moron of interviewer.

Lyrical Whisprings#2: All I need

I was listening to this song for quite sometime and was wondering, what the hell it means?
Then suddenly it came. I am not sure, if i am on the mark:

I am the next act
Waiting in the wings
I am an animal
Trapped in your hot car
I am all the days that you choose to ignore

I think, “I” is not a person, but its your innocence, its your playful spirit, who
has been lost in this material world. I know, you would say,
this theme has been repeated so many times, so
whats new?

Really isn’t it, “The song remains the same!”? True, I love the song.

Another paragraphs is:

I am a moth
Who justs wants to share your light
I am just an insect
Trying to get out of the dark
I will stick with you, because there are no others

Awesome. Is there a hope?

New release of BackgrounDRb available now

Assuming nobody is reading this, I would quietly mention that, I released new version of BackgrounDRb plugin today.
Checkout full announcement here:

http://rubyforge.org/pipermail/backgroundrb-devel/2007-November/001043.html

Here is a sample worker:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class FooWorker < BackgrounDRb::MetaWorker
  set_worker_name :foo_worker
  attr_accessor :count
 
  def create
    puts "Starting Foo Worker"
    @count = 0
    add_periodic_timer(4) { increment_status}
  end
 
  def process_request p_data
    user_input = p_data[:data]
    result = self.send(user_input[:method],user_input[:data])
    send_response(p_data,result)
  end
 
  def increment_status
    puts "Registering status"
    register_status("stuff #{rand(10)}")
  end
 
  def foobar
    puts "Invoking foobar at #{Time.now}"
  end
 
  def add_values user_input
    p user_input
    return eval(user_input)
  end
end
 
=begin
  problems, with existing things.
=end

#ruby-talk gems of the week

If you have any hopes of becoming a Ruby guru, then regular dose of ruby-talk is a must. In this post, I would try to summarize some of the cool stuffs being discussed on ruby-talk.

  • In Ruby, having method names that contain “-” can be PITA. So David Black and others were quick to point following approach of doing so:

    1
    2
    3
    4
    5
    
    irb(main):002:0> class C
    irb(main):003:1>   define_method("x-y") { puts "Weird method" }
    irb(main):004:1> end
    # At which point, the only way to call it is:
    irb(main):005:0> C.new.send("x-y")   # Weird method

    In other words, it’s not worth the trouble and you should find some
    other solution.

  • Trans asked on ruby-talk, how can we know, which files are loading or requiring this file. Ara pointed following solution:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    p Kernel.requiree('main')
     
    require 'b'
     
    p Kernel.requiree('main')
     
    BEGIN {
      module Kernel
        h = Hash.new
        define_method(:requiree) do |of|
          h[of]
        end
     
        r = method :require
        define_method(:require) do |*a|
          r.call *a
          h[a.first] = caller
        end
      end
    }

    The interesting bit around, should be noted.

    1
    
      r = method :require

    When you call method and pass method name as a parameter, it returns a closure. Although as pointed by someone it breaks “Rubygems”, and above approach shouldn’t be used. Rather, one should use:

    1
    
      alias_method :old_require, :require

Beware of raising exceptions

I was reading on twitter experience of scaling rails and use of dtrace. Unfortunately for GNU/Linux users “dtrace” is just a unreality. But if you just concentrate on the results, then you will find that, a major source of bottleneck was use of:

  foo = @someobj.amazing rescue "not_amazing"

Above code is in vogue, because it takes care of a lot of things like:

  • if @someobj is nil, then foo defaults to “not_amazing”.
  • if @someobj doesn’t support “amazing” method, foo defaults to “not_amazing”.
  • And of course the obvious case.

But that code succinctness comes at a cost and you may end up with deep stacktraces. This was a major bottleneck for twitter team.
Above snippet can be written as:

foo = @someobject.blank? ? "not_amazing" : (@someobject.respont_do?(:amazing) ? @someobject.amazing : "amazing")

Not so succinct, but does its job.

Read the details http://blogs.sun.com/ahl/entry/dtrace_for_ruby_at_oscon .

Useful discoveries of the day

This post is more of a “tumbler” style, but since I am lazy and doesn’t want to elaborate more and rather I would let respective links speak for themselves.

1. When you are working to create some sort of platform from scratch, you may find that your database is kinda stopping you from going full speed ahead. You may have several backend components and each of them interact with your database. But the problem with databases are, only so few connections it can spare and it becomes a bottlebeck afterwards. Geeks at newyork times( thats right, the newspaper guys) have developed a little utility called dbslayer, which lets you perform sql queries on database using JSON and it returns JSON responses. This is pretty neat and since it does this over HTTP, your database can scale. So, the idea is basically to use dbslayer layer in applications that are not too much into database and don’t basically need a persistence connections. Here is the link: http://code.nytimes.com/projects/dbslayer . And here is a little Ruby code sample, on how to perform a sql query using dbslayer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
require "rubygems"
require 'open-uri'
require "json"
 
@slayer_server = "localhost"
@slayer_port = "9090"
 
def query_url(sql)
  query_hash = { "SQL" => sql }
  url_args = URI.encode(query_hash.to_json)
  "http://#{@slayer_server}:#{@slayer_port}/db?#{url_args}"
end
 
def exec_query(sql)
  url = query_url(sql)
  open(url) do |f|
    yield JSON.parse(f.read)
  end
end
 
exec_query("select login from users") do |results|
  p results
end

2. In Ruby 1.9, matz has checked in support for Fluent Interfaces . Its pretty neat. David Flanganan talks about it here . David is the guy, who wrote “Javascript: the Definitive guide”. Good to see him working on Ruby.

3. Its hard to predict schedule of a software project because of so many uncertain things involved. Fred Brooks, Mythical man month is a milestone work on it. But looks like Joel’s team has done wonders in solving this uncertainty. Here Joel talks, how you can use Evidence bases scheduling for scheduling your project with reasonable accuracy. On a related note, here Joel talks, how having a active bug database is essential for a software project.

4. On edge rails, now you can refer fixtures as if they had associations. Its quite nice and makes job a lot easier for guys, who relied on fixtures for testing. There are certain cases against fixtures and how they make your tests “brittle”. But I have come to realize that, a combination of fixtures and mocks are probably best way to go. Here Pratik talks about latest code changes.

5. “A little Ruby” is a nice book for experienced Ruby developers who are looking to learn metaprogramming with Ruby. The sad part is, its not complete. Now, with growing interest in Ruby, the author wonders whether he should revive the project and complete the book. Vote for completion here

Using ‘?’ for wierdness

? is Ruby shorthand for referencing a character.

   irb(main):020:0> ?a => 97 
   irb(main):021:0> ?a == "a"[0] => true
   irb(main):014:0> ?? => 63 
   irb(main):015:0> !?? => false 
   irb(main):016:0> !!?? => true

Since ?? equals 63, it is equivalent to true in a boolean test. That means we can negate it with a bang!

Using class instance variables in ruby

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Object
  def self.metaclass; class << self; self; end; end
 
  def self.iattr_accessor *args
 
    metaclass.instance_eval do
      attr_accessor *args
      args.each do |attr|
        define_method("set_#{attr}") do |b_value|
          self.send("#{attr}=",b_value)
        end
      end
    end
 
    args.each do |attr|
      class_eval do
        define_method(attr) do
          self.class.send(attr)
        end
        define_method("#{attr}=") do |b_value|
          self.class.send("#{attr}=",b_value)
        end
      end
    end
  end
end

Now you can easily write code like this:

1
2
3
4
5
6
class Foobar
  iattr_accessor :hello
end
 
Foobar.hello = "I am a class instance atrribute"
p Foobar.hello