Dynamically set local variables in Ruby [duplicate]

As an additional information for future readers, starting from ruby 2.1.0 you can using binding.local_variable_get and binding.local_variable_set: def foo a = 1 b = binding b.local_variable_set(:a, 2) # set existing local variable `a’ b.local_variable_set(:c, 3) # create new local variable `c’ # `c’ exists only in binding. b.local_variable_get(:a) #=> 2 b.local_variable_get(:c) #=> 3 p a … Read more

SFINAE to check for inherited member functions

Take a look at this thread: http://lists.boost.org/boost-users/2009/01/44538.php Derived from the code linked to in that discussion: #include <iostream> template <typename Type> class has_foo { class yes { char m;}; class no { yes m[2];}; struct BaseMixin { void foo(){} }; struct Base : public Type, public BaseMixin {}; template <typename T, T t> class Helper{}; … Read more

easing c++ to objective-c/cocoa bridging via metaprogramming?

I didn’t find anything satisfactory and came up with a prototype that, given the following informal protocol: – (NSString*)concatString:(NSString*)s1 withString:(NSString*)s2; and this C++ code: struct CppClass { std::string concatStrings(const std::string& s1, const std::string& s2) const { return s1+s2; } }; std::string concatStrings(const std::string& s1, const std::string& s2) { return s1+s2; } allows creating and passing … Read more

Overriding special methods on an instance

Python usually doesn’t call the special methods (those with name surrounded by __) on the instance, but only on the class. (Although this is an implementation detail, it’s characteristic of CPython, the standard interpreter.) So there’s no way to override __repr__() directly on an instance and make it work. Instead, you need to do something … Read more

How to find out the arity of a method in Python

Module inspect from Python’s standard library is your friend — see the online docs! inspect.getargspec(func) returns a tuple with four items, args, varargs, varkw, defaults: len(args) is the “primary arity”, but arity can be anything from that to infinity if you have varargs and/or varkw not None, and some arguments may be omitted (and defaulted) … Read more

Ruby – print the variable name and then its value

Sure it is possible! My solution tests the var by Object#object_id identity: http://codepad.org/V7TXRxmL It’s crippled in the binding passing style … Although it works just for local vars yet, it can be easily be made “universal” adding use of the other scope-variable-listing methods like instance_variables etc. # the function must be defined in such a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)