convert string into class name in rails - ruby-on-rails

I have a variable handler whose value can be flight_gds, flight_commission etc.
There are classes in my application which has names like FlightGds, FlightCommission etc. under module FlightManager.
I want to execute the function of class based on the value of the handler.
The function name is same in all classes, only the class name is different which is dependent on
the handler.
For example:
If handler name is 'flight_gds', then the function is FlightManager::FlightGds.calculate()
I am trying something like this:
FlightManager::handler.camelize.calculate()
But I am getting undefined method handler error.
Is there any way to do this or I should go with if/else loop?

You should use constantize to get a class based on it's name in string:
"FlightManager::#{handler.camelize}".constantize.calculate())

Related

Swift function with a closure parameter seen as error type inside Unit Testing Module

I'm trying to write Unit tests for my Article Model. I have added the model as a test target.
I am able to access class functions without closure parameters in it but when I try to use a function with a closure, here's what Xcode's autocompletion gave
The full function prototype of 'fetchArticles' looks likes this:
class func fetchArticles(referringTile:Tile,completion:(articles:[Article])->(Void),
failure:(error:NSError)->(Void))
I've also tried declaring the function as 'public' but it didn't work.

this used inside class' method/function vs this used inside anonymous function?

Why "this" behaves differently when used inside a class function/method as compared to when it is used inside an anonymous function.
For example
public MyClass
{
function myfun()
{
output(this) // << will show the instance of this class but not myfun() function
abc = function ()
{
output ( this ) // << will show abc function
}
abc()
}
}
So, why "this" outputs the instance of MyClass but doesnot output myfun() . What makes it different from anonymous functions ?
While the specific will vary between languages, the general idea behind an anonymous inline method such as that is that the compiler will create a new class (possibly given some random name, just for it's own use), inside that class will be a method (again, probably given some auto-generated name for it's own use) and that method will do the work of the anonymous method. Then in the original call site of the anonymous method it creates an instance of this compiler generated class and calls the appropriate method within that class.
As such, the definition of that anonymous method is going to actually be called from within another type, not from within the type that declared the anonymous method.

Delphi Class Helper RTTI GetMethod

Lets say I have a sample class helper
TSampleClassHelper = class helper for TSampleClass
public
procedure SomeHelper;
end;
I do the following:
var
obj :TSampleClass;
begin
obj:=TSampleClass.Create;
obj.SomeHelper;
end;
and this works as expected.
But how can I use RTTI to invoke the helper method instead? The following does not seem to work, GetMethod returns nil.
var
obj :TSampleClass;
ctx :TRTTIContext;
rtype :TRTTIType;
rmethod :TRTTIMethod;
begin
obj:=TSampleClass.Create;
rtype:=ctx.GetType(obj.ClassType);
rmethod:=rtype.GetMethod('SomeHelper'); // rmethod is nil !
end;
So does RTTI not work for methods defined in class helpers? Is there anyway around this?
Thanks.
The reason your code returns a nil method is that the object's type does not contain a method named SomeHelper. The type that contains that method is the helper type.
So, you could write this which will return a non-nil method:
obj:=TSampleClass.Create;
rtype:=ctx.GetType(TypeInfo(TSampleClassHelper));
rmethod:=rtype.GetMethod('SomeHelper');
Of course, you should immediately see the first problem, namely the use of a compile time specified type, TSampleClassHelper. Can we use RTTI to discover TSampleClassHelper at run time based on the type of the instance? No we cannot, as I will explain below.
Even if we put that to one side, as far as I can see, there's no way to invoke the method using RTTI. If you call rmethod.Invoke(obj, []) then the code in TRttiInstanceMethodEx.DispatchInvoke blocks an attempt to call the helper method. It blocks it because it decrees that the type of the instance is not compatible with the class of the method. The pertinent code is:
if (cls <> nil) and not cls.InheritsFrom(TRttiInstanceType(Parent).MetaclassType) then
raise EInvalidCast.CreateRes(#SInvalidCast);
Well, you can obtain the code address of the helper method with rmethod.CodeAddress but you'll need to find some other way to invoke that method. It's easy enough to cast it to a method with the appropriate signature and invoke it. But why bother with rmethod.CodeAddress in any case? Why not use TSomeHelperClass.SomeMethod and cut RTTI out of the loop?
Discussion
Helper resolution is performed statically based on the active helper at the point of compilation. Once you attempt to invoke a helper method using RTTI there is no active helper. You've long since finished compiling. So you have to decide which helper class to use. At which point, you don't need RTTI.
The fundamental issue here is that class helper method resolution is fundamentally a static process performed using the context of the compiler. Since there is not compiler context at run time, class helper method resolution cannot be performed using RTTI.
For more insight into this have a read of Allen Bauer's answer here: Find all Class Helpers in Delphi at runtime using RTTI?

Groovy / Grails using a map as a parameter of a function

I have a very simple function which I define as follows:
def mySimpleFunction(Map myMap) {
// Function logic here...
}
However, when I try to compile this, I get a warning message and build exception which says that: The [mySimpleFunction] action accepts a parameter of type [java.util.Map] which has not been marked with Validateable.
How can I mark this function as Validateable? I imported the org.codehaus.groovy.grails.validation.Validateable
and have marked my class as #Validateable .
What should I be doing differently in order to get my application to build?
Thank you in advance!
If it is a helper method, make it private. In Grails 2.0+ public controller methods are assumed to be actions, and arguments are assumed to be bindable. That means they need to be number types, boolean, String, etc., or a command object class.
Command object classes are automatically made validateable if they're defined in the controller class file, and if they're defined elsewhere they need to be annotated as #Validateable.
Since this is a helper method and not an action, just make it private (especially since it can't be called from another class anyway):
private mySimpleFunction(Map myMap) {
// Function logic here...
}

Is this ZF2 constant var or datatype

public function setAlbumTable(AlbumTable $albumTable)
{
$this->albumTable = $albumTable;
return $this;
}
I am talking about first parameter ( it's not parameter btw) looks like datatype ? what is it ? constant ? I encounter this when trying to develop app in zend framework 2
This is PHP's type hinting. It means that the first parameter to this function - $albumTable - must be an instance of the AlbumTable class or a class that inherits from it.
class Car {}
class BlueCar extends Car {}
function drive_the_car(Car $car) {}
drive_the_car(42); // error, first parameter must be an instance of Car
drive_the_car(new stdClass()); // error
drive_the_car(new Car()); // works
drive_the_car(new BlueCar()); // works
The piece of code you're showing is an example of dependency injection via setter method. The setter is passed an instance of AlbumTable and assignes this instance to a class field.
Your method is passed ONLY ONE parameter: $albumTable.
The AlbumTable before the parameter is a type hint and makes sure that only a instance of AlbumTable or a deriving class can be passed to the setter.
It forces the actual parameter ($albumTable) to be an instance of AlbumTable class. PHP will give a fatal error if anything else is passed to the function.
This is useful so you don't have to check what type of variable/object you received in order to make use of it's functions and properties.

Resources