Archive for the 'Programming' Category
Improving your exception backtrace with Ruby DSLs
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
attr_accessor with default in 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
Meaning of ’self’ in 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!
More fun with Java (scope and duplicates)
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 postProtected within Java Enums
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
Why is XML Better?
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