Feb 2
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
Jan 31
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
Jan 25
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!
Sep 5
SWIG, Directors, and G++ optimization on RHEL 5.4
I couldn’t find anyone else who had this problem so in the event you are having a problem where your directors all have cPtr values of 0, I found on RHEL 5.4 that if you use directors and compile with optimization level above -01 suddenly (at least with target java) the swig objects representing the director classes will always have a null cPtr.
Please note that only the wrapper.cc file has to be compiled with optimization off to fix the problem.
No commentsJul 27
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….
No commentsJul 22
Protected 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
Jul 22
SWIG, Java, and JRuby
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
Nov 19
Opening a TUN Device on UNIX
Nov 15
Overview of the CCSDS Network Protocols
The CCSDS standards can be intimidating to the uninitiated, this article covers AOS, TC, COP-1, and other protocols used in space communications, how they interact, and where to find the details.
Read more