草庐IT

protected

全部标签

ruby-on-rails - 在 ActiveSupport::Concern 中访问包含类的 protected 常量

在ActiveSupport::Concern上下文中访问包含类的protected常量的最简单方法是什么?示例类:modulePrintableextendActiveSupport::Concernprivatedefprint_constantputsMY_CONSTANTendendclassPrinterincludePrintabledefprintprint_constantendprivateMY_CONSTANT='Hello'.freezeend此解决方案产生错误:NameError:uninitializedconstantPrintable::MY_CONSTA

ruby - Ruby 中的私有(private)/ protected block ?

Ruby似乎没有像这样定义protected/私有(private)block的工具:protecteddodefmethodendend与相比,这会更好protecteddefmethodendpublic您可能会忘记在protected方法之后“公开”的地方。似乎可以使用元编程来实现这一点。有什么想法吗? 最佳答案 由于您想按功能分组,您可以声明所有方法,然后使用protected后跟要保护的方法的符号来声明哪些方法是protected和私有(private)的,对于私有(private)方法也是如此。下面的类(class)说明

ruby - 在 Ruby 中访问 protected 方法

我正在尝试在Ruby中为自己使用访问修饰符。我有:classPersondefinitialize(first_name,last_name,age)@first_name=first_name@last_name=last_name@age=ageenddefshow()puts@first_nameputs@last_nameputs@ageendprotecteddefcompare(other)self.instance_variable_get(:@age)other.instance_variable_get(:@age)endendp1=Person.new("Some"

ruby-on-rails - 我们什么时候应该考虑使用 private 或 protected?

只是想知道,我们什么时候才真正必须对模型中的某些方法使用private或protected?有时我无法不在private或protected中对我的方法进行分组。我只是保持原样。但我知道这一定是一种不好的做法,否则这两个分组将不会在编程中创建。谢谢。 最佳答案 如果你打算在外部调用一个方法,record.method(),然后是“public”如果只在内部使用,self.method(),然后是“private”如果你计划在内部使用它,而且在后代中使用它,self.method()#insubclass,然后“protected”

ruby-on-rails - attr_accessible 在 rails Active Record 中

当我使用attr_accessible指定我将公开模型中的哪些字段时,脚本/控制台也是如此吗?我的意思是我没有指定为attr_accessible的东西也不能通过控制台访问? 最佳答案 这仅适用于批量分配。例如,如果您要在模型中设置attr_protected:protected:>>Person.new(:protected=>"test")=>#相反,您可以使用attr_accessible将您想要的所有属性设置为可访问。但是,以下内容仍然有效:>>person=Person.new=>#>>person.protected="

ruby - 为什么不能使用要处理的符号调用 protected 方法?

给定以下类:classFoodefadup.tap{|foo|foo.bar}enddefbdup.tap(&:bar)endprotecteddefbarputs'bar'endend看起来Foo#a和Foo#b应该是等价的,但它们不是:>Foo.new.abar=>#>Foo.new.bNoMethodError:protectedmethod`bar'calledfor#这是有原因的吗?这是错误吗?在Ruby2.2.3p173上测试 最佳答案 让我们首先注意,在Ruby中,您可能知道,在类Foo上声明的方法a中,我可以在任何对

ruby - 回应?和 protected 方法

respond_to可能不是那么明显?在ruby中工作。考虑一下:classAdefpublic_methodendprotecteddefprotected_methodendprivatedefprivate_methodendendobj=A.newobj.respond_to?(:public_method)#true-that'sprettyobviousobj.respond_to?(:private_method)#false-asexpectedobj.respond_to?(:protected_method)#true-WTF?因此,如果“obj”响应我们应该期望的

ruby-on-rails - 警告 : Can't mass-assign protected attributes

当发帖到/:username/about时,我收到“警告:无法批量分配protected属性:about”。classAbout["lower(username)=?",params[:username].downcase])iftrue@about=@user.aboutif@about.update_attributes(params[:about])flash[:notice]="Successfullyupdatedpost."respond_with(@about,:location=>about_path(@about.user.username))elseredirect

ruby - "private"、 "public"和 "protected methods"之间有什么区别?

我正在学习Ruby,但已经到了让我感到困惑的地步。我正在使用的书上讲的是private、public和protectedmethods,但我还是有点糊涂。它们之间有什么区别? 最佳答案 公共(public)-可以从任何地方调用Private-不能在类作用域外调用该方法。对象只能给自己发送消息例如:面包师有bake方法是公开的,但break_eggs是私有(private)的protected-只要默认对象self是与您正在调用其方法的对象相同的类的实例,您就可以调用对象的protected方法例如:使用n保护方法,c1可以要求c2执

ruby-on-rails - Rails 中的 protected 和私有(private)方法

Ruby中的方法可见性(公共(public)、protected和私有(private)方法)在thisblogpost等地方得到了很好的解释。.但在RubyonRails中,由于框架的设置方式,它似乎与在常规Ruby应用程序中略有不同。那么,在Rails模型、Controller、助手、测试等中,什么时候适合/不适合使用protected或私有(private)方法?编辑:感谢您到目前为止的回答。我了解Ruby中protected和private的概念,但我正在寻找更多关于在Rails应用程序的各个部分(模型、Controller、助手、测试)的上下文中使用这些类型的可见性的典型方式