I have 2 classes whose object should act as "partners". The first one is my Thing class, whose instances should act as Tree::TreeNodes of the gem RubyTree.
Basically, this delegation can be implemented using Forwardable:
class Thing < NoClassInheritancePlease
extend Forwardable
def initialize(title = "node")
@node = Tree::Tree...
Has anyone seen metamorphic code -- that is, code that generates and runs instructions (including IL and Java Bytecode, as well as native code) -- used to reduce boilerplate code?
Regardless of the application or language, typically one has some database code to get rows from the database and return a list of objects. Of course, there ...
I am creating non-namespaced classes dynamically.
I have seen an example where this creation is done within the Object class:
class Object
for elem in ARRAY
sub_class = Object.const_set(elem.to_s.camelize, Class.new(SuperClass))
sub_class.class_eval do
def initialize(*args, &block)
...
super *args, &b...
Postsharp is great, but only the 1.5 version is still opensource. Does it work with .net 4.0? If not, are there any other good AOP weavers out there? I'm not interested in the proxy type.
...
Could the own heap space be readed? could the software be self modified in memory?
I write some code to show the subject,
am I reading own code at memory?
how (if possible) to write it and change instruction on runtime?
#include<stdio.h>
#include<stdint.h>
volatile int addressBase;
uint8_t read(int address);
int main(void) {
...
I am reading Metaprogramming in Ruby book. In that book, when I was reading about scopegates, the following code was shown
my_var = "Success"
MyClass = Class.new do
puts "#{my_var} in the class definition"
define_method :my_method do
puts "#{my_var} in the method"
end
end
MyClass.new.my_method
=>Success in the class defini...
I'm trying to use the type(,,) function to dynamically build a module. The module creates classes representing templates, and I need a new class for every .tex file that lives in a particular folder. For instance, if I have a a4-page-template.tex file, I need to create a class called A4PageTemplate.
I can create the type easily enough u...
I've read:
Wikipedia
Code Generation vs. Metaprogramming
The art of Metaprogramming
Metaprogramming at c2.com
and I confess some confusion at the purpose behind metaprogramming/code generation.
Does anyone have a concrete example of where they use metaprogramming/code generation? Even better would be an accompanying explanation of w...
Is there a way to know whether or not a method has been overridden by a subclass programmatically? Something that works like this:
class BaseModel
def create
puts "superclass"
end
end
class SomeModel < BaseModel
def create
puts "subclass"
end
end
puts SomeModel.overridden_instance_methods #=> [:create]
Any ideas?
...
How would you list out the modules that have been included in a specific class in a class hierarchy in Ruby? Something like this:
module SomeModule
end
class ParentModel < Object
include SomeModule
end
class ChildModel < ParentModel
end
p ChildModel.included_modules #=> [SomeModule]
p ChildModel.included_modules(false) #=> []
Li...
Hey all, I'm currently trying to write a compile-time string encryption (using the words 'string' and 'encryption' quite loosely) lib.
What I have so far is as follows:
// Cacluate narrow string length at compile-time
template <char... ArgsT>
struct CountArgs
{
template <char... ArgsInnerT> struct Counter;
template <char Cur, char.....
Is there a way to programmatically create stored procedures using MySQL? I can write a stored procedure to create databases and tables using prepared statements, but I get an error message saying it is not supported to create stored procedures with prepared statements.
I realize I can do this in PHP or Java, but so far I have been kee...
A while back, I needed a solution to sanely import libraries in VBScript.
VBScript, for reference, has no build-in import capabilities. The traditional method of importing files is to use SSI, which dumps the contents of the includee verbatim into the includer. This is less-than-optimal for a number of reasons: there is no way to avoid ...
test = "a"
test.class_eval do
# what is going on here???
end
...
My question does not really have much to do with sqlalchemy but rather with pure python.
I'd like to control the instantiation of sqlalchemy Model instances. This is a snippet from my code:
class Tag(db.Model):
__tablename__ = 'tags'
query_class = TagQuery
id = db.Column(db.Integer, primary_key=True)
name = db.Column(d...
Given the following template:
template<class T>
class Container
{
private:
boost::function<T> f;
};
... and its instantiation, perhaps as follows:
Container<bool(int, int)> myContainer;
, is there a way to access the return type of the function description and compile conditionally against it? For example, if the caller...
I would like to do something like this from a Ruby script, within a loop:
Write a file a.rb (which changes each iteration)
execute system(ruby 'a.rb')
a.rb writes a string with results to a file 'results'
a.rb finishes and Ruby returns 'true' (assuming no errors)
the calling script reads the file 'results' and takes action.
I expect ...
Possible Duplicate:
How do I use define_method to create class methods?
I am trying to do this:
class Foo
class << self
def runtime_method(*methods)
methods.each do |name|
define_method "self.#{name}" do |*args|
"dynamic class method #{name.inspect}"
end
self.class_eval do
...
I'm trying to write a method that tells me every class that includes a particular Module. It looks like this -
def Rating.rateable_objects
rateable_objects = []
ObjectSpace.each_object(Class) do |c|
next unless c.include? Rateable
rateable_objects << c
end
rateable_objects
end
Where "Rateable" is my module that I'm in...
I'm looking to take an arbitrary number of lists (e.g. [2, 1, 4 . . .], [8, 3, ...], . . .) and pick numbers from each list in order to generate all permutations. E.g.:
[2, 8, ...],
[2, 3, ...],
[1, 8, ...],
[1, 3, ...],
[4, 8, ...],
[4, 3, ...],
...
This is easily accomplished using nested for-loops, but since I'd like to it accept...