是否有 ruby 等同于 php __invoke 魔术方法?
Is there a ruby equivalent to the php __invoke magic method?
在ruby中是否有与php__invoke方法等效的方法?
例如
class Test {
public function __invoke() {
echo "invoked";
}
}
$test = new Test();
$test(); // prints invoked
不完全一样,但它应该可以完成工作
class Test
def self.call
puts "invoked self.call"
return new
end
def call
puts "invoked call"
end
end
t = Test.()
t.()
您可以在 类 和对象上使用 .()
语法,因为 类 是对象。 .()
只是 shorthand 对于 .call
在ruby中是否有与php__invoke方法等效的方法?
例如
class Test {
public function __invoke() {
echo "invoked";
}
}
$test = new Test();
$test(); // prints invoked
不完全一样,但它应该可以完成工作
class Test
def self.call
puts "invoked self.call"
return new
end
def call
puts "invoked call"
end
end
t = Test.()
t.()
您可以在 类 和对象上使用 .()
语法,因为 类 是对象。 .()
只是 shorthand 对于 .call