17 Jul, 2009, Chris Bailey wrote in the 1st comment:
Votes: 0
Ruby has of course made several avenues of dynamic programming or metaprogramming easy to use. While the safety of some of these methods is questionable they can still be power tools when used properly. Consider some of the following examples from The Ruby Way by Hal Fulton:
Using const_get The const_get method will accept a string, and return the value of a constant matching that string. This can of course be emulated using eval, but this is a bit stricter and better to use if your only purpose is to gather a constant value.
str = "PI" Math.const_get(str) # Evaluates to Math::PI
Dynamically instantiating a Class from a string. Given a string containing a name of a class, we can dynamically create an instance of that class.
classname = "Array" klass = Object.const_get(classname) x = klass.new(4,1) # [1,1,1,1]
Getting and setting instance variables Ruby has methods that can retrieve or assign instance variable values when given the name of the instance variable as a string.
class MyClass attr_reader :alpha, :beta def initialize(a,b,g) @alpha, @beta, @gamma = a, b, g end end x = MyClass.new(10,11,12)
x.instance_variable_set("@alpha",234) p x.alpha # 234 v = x.instance_variable_get("@alpha") # 234
Next time I'll go over define_method, method_missing, const_missing and remove_const
instance_variable_set and instance_variable_get also can circumvent basic abstraction techniques. They should be used with that in mind.
17 Jul, 2009, Chris Bailey wrote in the 3rd comment:
Votes: 0
Absolutely Runter. That is one of the biggest things that people use to dispute the use of those methods, but I think it's an important feature for metaprogramming. =)
Absolutely Runter. That is one of the biggest things that people use to dispute the use of those methods, but I think it's an important feature for metaprogramming. =)
I'm a Ruby naysayer. :)
17 Jul, 2009, Chris Bailey wrote in the 5th comment:
I use a fair bit of metaprogramming in TeensyMud, not nearly as much as something like MUES does. Making Ruby Logging Simple with Log4r Heh. I should just let others document my code as they do a fair bit better than myself. Anyway the idea wasn't just to make logging simple, but to do class-level debugging. I'm not all that thrilled with Log4r though, and was considering going back to the stdlib Logger.
Using const_get
The const_get method will accept a string, and return the value of a constant matching that string. This can of course be emulated using eval, but this is a bit stricter and better to use if your only purpose is to gather a constant value.
Dynamically instantiating a Class from a string.
Given a string containing a name of a class, we can dynamically create an instance of that class.
Getting and setting instance variables
Ruby has methods that can retrieve or assign instance variable values when given the name of
the instance variable as a string.
Next time I'll go over define_method, method_missing, const_missing and remove_const