How can I make an object callable?

Viewed 1259

Is there a way to create a object that has properties and can be called using only the notation of a function call? Something equivalent to Python's __call__.

For example:

obj = ExampleClass.new()
obj() # call notation

Alternate approaches are welcome, I need a way for the callable "object" to store its own properties.

What I am trying to do is store a callable "object" in a variable. This object has properties that are associated with it, while at the same time, it can be used exactly like a method.

3 Answers

One thing that u can do is simple create an class and a function with the same name:

class Thing end

def Thing() end

Thing.new Thing()

Related