开发者

What is Object::private and Object::public in Ruby?

开发者 https://www.devze.com 2023-04-11 09:04 出处:网络
What are these methods and how bad is it to override them? irb(main):001:0> Object::respond_to?(\'private\', true)

What are these methods and how bad is it to override them?

irb(main):001:0> Object::respond_to?('private', true)
=> true

irb(main):002:0> Object::respond_to?('public', true)
=> true

The problem comes up in Rails when trying to define a scope named private or public for a model. Because of the fix for bug https://rails.lighthouseapp.com/projects/8994/tickets/4167-activerecord-named_scope-using-columns-as-the-name-is-buggered now there's a lot of warnings like:

Creating scope开发者_如何学Python :public. Overwriting existing method MyModel.public.


The public and private methods are actually ruby's access modifiers.

Basically, when you do this:

class Example
  public

  def something
  end

  private

  def something_else
  end
end

The public and private keywords are actually not keywords at all, they're method calls. I'm pretty sure it's not a good idea to override them, so I'd name the scopes in some other way.

0

精彩评论

暂无评论...
验证码 换一张
取 消