How to call methods dynamically based on their name?
What you want to do is called dynamic dispatch. It’s very easy in Ruby, just use public_send
:
method_name = 'foobar'
obj.public_send(method_name) if obj.respond_to? method_name
If the method is private/protected, use send
instead, but prefer public_send
.
This is a potential security risk if the value of method_name
comes from the user. To prevent vulnerabilities, you should validate which methods can be actually called. For example:
if obj.respond_to?(method_name) && %w[foo bar].include?(method_name)
obj.send(method_name)
end
How do I call a dynamically-named method in Javascript?
Assuming the populate_Colours
method is in the global namespace, you may use the following code, which exploits both that all object properties may be accessed as though the object were an associative array, and that all global objects are actually properties of the window
host object.
var method_name = "Colours";
var method_prefix = "populate_";
// Call function:
window[method_prefix + method_name](arg1, arg2);
Call a method on a dynamic object by name
Turns out it's not that hard after all. I didn't know if normal reflection would work with dynamic
types. All resources I found for dynamic objects involved overriding TryInvokeMember
, which wasn't an option. Here's missing code:
var method = ((object)dynamicObject).GetType().GetMethod(methodName);
method.Invoke(dynamicObject, args);
How to call Method's Class Name dynamically?
Just make a dictionary with instances of the differenct codecs, initialize the dictionary a single time with all codecs. And then get any codec by name whenever you need it. Each codec must be a separate non-static class implementing a ICodec interface you create.
Example, unvalidated c#, to give you the gist:
private static Dictionary<string, ICodec> _codec;
public static void Initialize()
{
_codec = new Dictionary<string, ICodec> {
{ "Vorbis", new VorbisCodec() }
{ "Opus", new OpusCodec() }
};
}
public static void SetControls(string codecName)
{
_codec[codecName].set();
}
public interface ICodec
{
void set();
}
Addition as you commented to have it even more compact:
You can also use reflection to get a class by name, instantiate it and then call the .set() method:
((ICodec) Activator.CreateInstance(Assembly.GetExecutingAssembly().GetType(codecClassNameHere))).set();
I advise against it though. Code should also be readable. The Dictionary approach shows very cleanly what's going on. Reflection hides that, this is often more annoying for maintaining the code later on, than the "coolness" of making it very compact with reflection now :)
How to dynamically pass name of calling method as a parameter in the same method?
You could look at GetCurrentMethod
MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
string methodName = method.Name;
string className = method.ReflectedType.Name;
string fullMethodName = className + "." + methodName;
But i assume that using that kind of ,,thing'' is not good solution for anything. I think you have misarchitected you app
Related Topics
Difference Between Attr_Accessor and Attr_Accessible
Rails 4: List of Available Datatypes
What Are All the Common Ways to Read a File in Ruby
How to Update Ruby Version 2.0.0 to the Latest Version in MAC Osx Yosemite
Methods in Ruby: Objects or Not
Actioncontroller::Unknownformat
Rails 3, Has_One/Has_Many With Lambda Condition
What Do 'I' and '-I' in Regex Mean
Cannot Load Such File - Zlib Even After Using Rvm Pkg Install Zlib
What's the Difference Between Ruby'S Dup and Clone Methods
Best Way to Create Custom Config Options For My Rails App
Ruby: Kind_Of? Vs. Instance_Of? Vs. Is_A
Incompatible Character Encodings: Ascii-8Bit and Utf-8