PHP 魔法方法 __call、__get 和 __set 的 Ruby 等效项

发布于 2024-09-14 09:10:22 字数 190 浏览 16 评论 0原文

我非常确定 Ruby 具有这些(相当于 __call、__get__set),否则 find_by 在 Rails 中如何工作?也许有人可以举一个简单的例子来说明如何定义与 find_by 相同的方法?

谢谢

I am pretty sure that Ruby has these (equivalents for __call, __get and __set), because otherwise how find_by would work in Rails? Maybe someone could give a quick example of how to define methods that act same as find_by?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-09-21 09:10:22

简而言之,您可以将

  • __call 映射到带有参数 __set 的 method_missing 调用
  • ,映射到方法名称以 '=' 结尾的 method_missing 调用
  • __get 映射到不带任何参数的 method_missing

调用 __call

php

class MethodTest {
  public function __call($name, $arguments) {
    echo "Calling object method '$name' with " . implode(', ', $arguments) . "\n";
  }
}

$obj = new MethodTest;
$obj->runTest('arg1', 'arg2');

ruby

class MethodTest
  def method_missing(name, *arguments)
    puts "Calling object method '#{name}' with #{arguments.join(', ')}"
  end
end

obj = MethodTest.new
obj.runTest('arg1', 'arg2')

​​ __set 和 __get

php

class PropertyTest {
  //  Location for overloaded data.
  private $data = array();

  public function __set($name, $value) {
    echo "Setting '$name' to '$value'\n";
    $this->data[$name] = $value;
  }

  public function __get($name) {
    echo "Getting '$name'\n";
    if (array_key_exists($name, $this->data)) {
      return $this->data[$name];
    }
  }
}

$obj = new PropertyTest;
$obj->a = 1;
echo $obj->a . "\n";

ruby

class PropertyTest

  # Location for overloaded data.
  attr_reader :data
  def initialize
    @data = {}
  end

  def method_missing(name, *arguments)
    value = arguments[0]
    name = name.to_s

    # if the method's name ends with '='
    if name[-1, 1] == "="

      method_name = name[0..-2]
      puts "Setting '#{method_name}' to '#{value}'"
      @data[method_name] = value

    else

      puts "Getting '#{name}'"
      @data[name]

    end
  end

end

obj = PropertyTest.new
obj.a = 1 # it's like calling "a=" method : obj.a=(1)
puts obj.a

in short you can map

  • __call to a method_missing call with arguments
  • __set to a method_missing call with method's name ending with '='
  • __get to a method_missing call without any arguments

__call

php

class MethodTest {
  public function __call($name, $arguments) {
    echo "Calling object method '$name' with " . implode(', ', $arguments) . "\n";
  }
}

$obj = new MethodTest;
$obj->runTest('arg1', 'arg2');

ruby

class MethodTest
  def method_missing(name, *arguments)
    puts "Calling object method '#{name}' with #{arguments.join(', ')}"
  end
end

obj = MethodTest.new
obj.runTest('arg1', 'arg2')

__set and __get

php

class PropertyTest {
  //  Location for overloaded data.
  private $data = array();

  public function __set($name, $value) {
    echo "Setting '$name' to '$value'\n";
    $this->data[$name] = $value;
  }

  public function __get($name) {
    echo "Getting '$name'\n";
    if (array_key_exists($name, $this->data)) {
      return $this->data[$name];
    }
  }
}

$obj = new PropertyTest;
$obj->a = 1;
echo $obj->a . "\n";

ruby

class PropertyTest

  # Location for overloaded data.
  attr_reader :data
  def initialize
    @data = {}
  end

  def method_missing(name, *arguments)
    value = arguments[0]
    name = name.to_s

    # if the method's name ends with '='
    if name[-1, 1] == "="

      method_name = name[0..-2]
      puts "Setting '#{method_name}' to '#{value}'"
      @data[method_name] = value

    else

      puts "Getting '#{name}'"
      @data[name]

    end
  end

end

obj = PropertyTest.new
obj.a = 1 # it's like calling "a=" method : obj.a=(1)
puts obj.a
执手闯天涯 2024-09-21 09:10:22

动态查找器是通过实现缺少

http://ruby-doc.org/ 的 方法来完成的core/classes/Kernel.html#M005925

看一下这篇博文,它会告诉你它们如何工作的要点。

http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work

The dynamic finders are done by implementing method missing

http://ruby-doc.org/core/classes/Kernel.html#M005925

Take a look at this blog post, it will give you the gist of how they work..

http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文