Archive for the 'Programming' Category

Improving your exception backtrace with Ruby DSLs

February 02nd, 2011 | Category: Programming, Ruby

One issue with a Domain Specific Language in any language is that the error messages from the actual language tend to ruin the illusion of a custom language. In ruby a stack trace with a million “method_missing” calls can be confusing.
So what can you do? Edit and filter the backtrace.
Read more

Comments are off for this post

attr_accessor with default in Ruby

January 31st, 2011 | Category: Programming, Ruby

If you aren’t in rails (hello to both of you) the lack of an attr_accessor with a default value is very annoying. I wrote up the following:

module ExtendAccessors
  def attr_accessor_with_default name, *default, &block
    if(default.size >= 1)
      define_method name.to_sym do
        instance_variable_set("@#{name}", default[0]) unless instance_variable_defined?("@#{name}")
        instance_variable_get("@#{name}")
      end
    elsif block_given?
      define_method name.to_sym do
        instance_variable_set("@#{name}", instance_eval(&block)) unless instance_variable_defined?("@#{name}")
        instance_variable_get("@#{name}")
      end
    else
      raise "Must either provide a default value or a default code block"
    end
    define_method "#{name}=".to_sym do |value|
      instance_variable_set("@#{name}",value)
    end
  end
end

which supports the following syntax:

class A
  extend ExtendAccessors
  attr_accessor_with_default :some_map, {}
  attr_accessor_with_default :some_object do
    AnotherObject.new(self)
  end
end
Comments are off for this post

Meaning of ’self’ in Ruby

January 25th, 2011 | Category: Programming, Ruby

This came up today. When defining classes in Ruby “self” can refer to the class or the instance of that class depending on the context and understanding which is which is important.

When defining a class “self” in the context of the class definition refers to the object representing the class being defined. When in a (non-class-level) method “self” refers to the instance of the object. So:

class SomeClass
  #dynamically add a method to the SomeClass object. 
  #self is the SomeClass class object
  #now I can call SomeClass.class_level_method
  def self.class_level_method
    self == SomeClass #this is true....
  end
 
  #define an instance level method
  def instance_level_method
    self #this is now an instance of the object
  end
end

This syntax may make more sense in terms of the following

class SomeClass
end
 
#this can be done outside of the class definition
def SomeClass.class_level_method
  echo "hi"
end
 
c = SomeClass.new
d = SomeClass.new
 
#class objects are notspecial in this regard. 
#You can do the same thing to
#a specific object instance of any other type.
def c.specific_instance_method
  #this method is defined just for this 
  #specific instance of this class
end
 
c.specific_instance_method #no problem
d.specific_instance_method #nope!
Comments are off for this post

More fun with Java (scope and duplicates)

July 27th, 2010 | Category: C++, Java, Programming

I thought this was interesting… In Java, a block scoped variable may conflict with a local variable declared later in method scope, but not previously (as shown below):

class A{
  public static void main(String args[]){
    {int k = 5;}
    int k = 12; //this is OK
    {int k = 22;}//this is not....
  }
}

This is not the same as C++ where this compiles happily:

int main(){
  {int i;}
  int i;
  {int i;}
  return 0;
}

Usually C++ has the more obscure behavior….

Comments are off for this post

Protected within Java Enums

July 22nd, 2010 | Category: Java, Programming

I just noticed that java enums allow protected member variables and methods. Why is this interesting? Can you think of any code that might actually make use of a protected member in an enum (as opposed to private)? Example after the jump (warning, not practical…).
Read more

Comments are off for this post

SWIG, Java, and JRuby

July 22nd, 2010 | Category: C++, Java, Networking, Programming, Ruby

Providing a robust, maintainable, and interactive interface to your C/C++ application can be a challenge, but I’ve found that a combination of SWIG, Java, and JRuby (or Jython if you prefer) makes for a very powerful combination.
Read more

Comments are off for this post

Opening a TUN Device on UNIX

November 19th, 2009 | Category: C, Networking, Programming

The TUN/TAP interface under Linux provides user space access to Transport (Ethernet) or Network Layer (IP) traffic by allowing a developer to create a “virtual” interface that can be openend in user space as a file descriptor.
Read more

1 comment

RSS and mercurial

October 27th, 2009 | Category: Programming

In a surprisingly well-kept secret, HgWeb and HgWebDir support RSS. Details below.
Read more

1 comment

Why is XML Better?

July 24th, 2009 | Category: Programming, XML, XPath, XSLT

Why is XML better than custom internal formats? Isn’t a non-standard set of XML tags basically equivalent to a custom format when using data internally to an application? Absolutely not, and here is why.
Read more

Comments are off for this post

DVSL: An Alternative to XSLT

June 26th, 2009 | Category: Java, XML, XPath, XSLT

DVSL is a fairly small, little known, product associated with the Velocity project over at Apache which takes the best part of XSLT: XPATH, and replaces the verbose and frustrating scripting of XSLT with a java-based template language instead.
Read more

Comments are off for this post

« Previous PageNext Page »