I'm sorting array:
myArray.sort(comparators.some_comparator);
and I have several comparator to choose from:
comparators = {
asc_firstname_comparator : function(o1, o2){
...
}
desc_firstname_comparator : function(o1, o2){
...
}
etc...
}
I want to write function which returns certain comparator depending on ...
I don't understand class_eval in ruby.
class Module
def attr_ (*syms)
syms.each do |sym|
class_eval %{def #{sym}= (val)
@#{sym} end} = val
end
end
end
What does the % mean?
What does class_eval do?
And where is (val) coming from?
Thanks
...
It is easy given a container to get the associated iterators, example:
std::vector<double>::iterator i; //An iterator to a std::vector<double>
I was wondering if it is possible, given an iterator type, to deduce the type of the "corresponding container" (here I am assuming that for each container there is one and only one (non-const) ...
Consider the following extension (the pattern popularized by several Rails plugins over the years):
module Extension
def self.included(recipient)
recipient.extend ClassMethods
recipient.send :include, InstanceMethods
end
module ClassMethods
def macro_method
puts "Called macro_method within #{self.name}"
end
...
Hi,
I'm trying to apply a transformation to an mpl::string, but can't get it to compile. I'm using MS VC++2010 and Boost 1.43.0. The code:
#include <boost/mpl/string.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/arithmetic.hpp>
using namespace boost;
int ma...
I want to do some metaprogramming in a statically typed language, where both my programs and my meta-programs will be typed. I mean this in a strong sense: if my program generator compiles, I want the type system to be strong enough that only type-correct programs can be generated.
As far as I know, only metaocaml can do this. (No, ne...
Hi all; I've multiply heard Ruby touted for its super spectacular meta-programming capabilities, and I was wondering if anyone could help me get started with this problem.
I have a class that works as an "archive" of sorts, with internal methods that process and output data based on an input. However, the items in the archive in the cl...
I want to use the value v inside of an instance method on the metaclass of a particular object:
v = ParserMap[kind][:validation] # We want to use this value later.
s = ParserMap[kind][:specs]
const_set(name, lambda {
p = Parser.new(&s)
# This line starts a new scope...
class << p
define_method :validate do |opts|
v.ca...
For about a year I have been thinking about writing a program that writes programs. This would primarily be a playful exercise that might teach me some new concepts. My inspiration came from negentropy and the ability for order to emerge from chaos and new chaos to arise out of order in infinite succession.
To be more specific, the ...
Hi,
I have the following code in my project`s lib directory
module Pasta
module ClassMethods
def self.has_coordinates
self.send :include, InstanceMethods
end
end
module InstanceMethods
def coordinates
[longitude ||= 43.0, latitude ||= 25.0]
end
end
ActiveRecord::Base.extend ClassMethods
...
For a project at university I plan to build an annotation based tool to visualize/play around with data structures.
Here's my idea:
Students which want to try out their self-written data structures need to:
mark the type of their data structures using some sort of marker annotation e.g.
@List
public class MyList { ... }
so that I k...
This piece of code is supposed to calculate an approximation to e (i.e. the mathematical constant ~ 2.71828183) at compile-time, using the following approach;
e1 = 2 / 1
e2 = (2 * 2 + 1) / (2 * 1) = 5 / 2 = 2.5
e3 = (3 * 5 + 1) / (3 * 2) = 16 / 6 ~ 2.67
e4 = (4 * 16 + 1) / (4 * 6) = 65 / 24 ~ 2.708
...
e(i) = (e(i-1).numer * i + 1) /...
I have a class Wrapper that supports adding options that you can then look up later. It stores these options in an internal hash @dict.
w = Wrapper.new
w.foo # => NameError
w.foo = 10
w.foo # => 10
How can I write a method_missing for Wrapper so that I can support nested calls on @dict?
w = Wrapper.new
w.foo.bar.baz = 1000
w.foo.bar....
I am running a bit of code that looks like this:
result = system("ruby " + filename_random_ruby_script)
if result
save_to_disk(random_ruby_script)
else
# Do Nothing
end
The variable "random_ruby_script" represents any .rb file.
This code is the first of many calls to system() and runs a ruby file that may also contain calls to sy...
Can I, in the GNU Objective-C runtime, attach semi-arbitrary pieces of data to instance variables?
Challenge:
I'm currently working on a kind of Cocoa workalike for Linux, as a sort of pet project. (Please, let's not get sidetracked by all the "use GNUStep" stuff. I know about it, but it doesn't suit my needs. Moving on…) For this purp...
I do not understand how this piece of code (from Wikipedia) works:
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
int x = Factorial<4>::value; // == 24
i...
Is there any way, to track dynamically which classes or modules are included in required files.
a short example:
#my_module.rb
module MyCustomModule
def foo
end
end
#custom.rb
require 'my_module.rb'
#is here any way to track which modules are in the required 'my_module.rb'
#without parsing the file or having some naming conventio...
This syntax was used as a part of an answer to this question:
template <bool>
struct static_assert;
template <>
struct static_assert<true> {}; // only true is defined
#define STATIC_ASSERT(x) static_assert<(x)>()
I do not understand that syntax. How does it work?
Suppose I do
STATIC_ASSERT(true);
it gets converted to
static_a...
Hi, I want to eliminate "=" sign for a particular reason. It might looks like this:
cat_that_has_name("Kelly").as(:kelly)
kelly.do_something
The "as" method here is used to generate a method "kelly" that reference my cat. Could anyone help me with this?
Any suggestions will be appreciated.
Update:
Jorg was right, I've add a simple...
Hi,
I was reading up on rspec and I was trying to figure out how rspec's "should" was implemented
Could someone give a hand on how the meta nature of this function works?
The code is located here:
http://github.com/dchelimsky/rspec/blob/master/lib/spec/expectations/extensions/kernel.rb
TIA,
-daniel
Clarification:
target.should =...