草庐IT

pe_attr_page_faults

全部标签

ruby - 使用 attr_accessor 动态创建类属性

在Ruby中,有没有办法动态地向类中添加实例变量?例如:classMyClassdefinitializecreate_attribute("name")enddefcreate_attribute(name)attr_accessorname.to_symendendo=MyClass.newo.name="Bob"o.name 最佳答案 一种方法(还有其他方法)是这样使用instance_variable_set和instance_variable_get:classTestdefcreate_method(name,&bloc

Ruby attr_accessor 与 getter/setter 基准测试 : why is accessor faster?

我刚刚针对等效的getter/setter方法测试了attr_accessor:classA#wedefinetwoR/Wattributeswithaccessorsattr_accessor:acc,:bcc#wedefinetwoattributeswithgetter/setter-functionsdefdirA=(d);@dirA=d;enddefdirA;@dirA;enddefdirB=(d);@dirB=d;enddefdirB;@dirB;endendvarA=A.newstartT=0dirT=0accT=0#nowwedo100timesthesamebench

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 - 如何将 octopress 3 部署到现有 gh-pages 站点中的子目录?

我正在使用Octopress3,当我运行jekyllbuild时,它会生成正确的文件集(包括我的静态文件,请参阅下面的文件列表):$cd_site:_site$ls-a.CNAMEassetsgoogle2d8.htmlindex-alternative.html..aboutblogincrease-revenue.htmlindex.html请注意,我的整个jekyll生成的blog现在都安全地存储在\blog\中,这正是我想要的。但是一旦我执行octopressdeploy,它就会覆盖整个文件夹(这也会覆盖我现有的静态文件),请参见下面的list:$cd_site:_site$l

ruby-on-rails - Rails 返回 : [BUG] Segmentation fault

如果我运行railss,我得到:/Users/adam/.rvm/gems/ruby-1.9.3-p327/gems/pg-0.13.2/lib/pg_ext.bundle:[BUG]Segmentationfaultruby1.8.7(2012-02-08patchlevel358)[universal-darwin12.0]Aborttrap:6版本:rails-vRails3.2.1ruby-vruby1.9.3p327(2012-11-10revision37606)[x86_64-darwin12.2.0]如果我使用1.9.3,为什么在错误消息中提到ruby​​版本1.8.7

ruby - 数组的 attr_accessor?

我想使用attr_accessor将数组作为实例变量。但是attr_accessor不只是用于字符串吗?如何在阵列上使用它?更新:例如。如果你想:object.array="cat"object.array="dog"ppobject.array=>["cat","dog"]那么你必须自己创建这些方法吗? 最佳答案 classSomeObjectattr_accessor:arraydefinitializeself.array=[]endendo=SomeObject.newo.array.push:ao.array.push:b

ruby-on-rails - 在 Rails 3 中使用 current_page 时为 "No routes matches"

有没有人遇到过使用current_page时路由神秘地变得无法检测到?在Rails3中?即使使用包含路由、View和Controller的完全生成的脚手架,我也会收到“无路由匹配”错误。代码如下:ifcurrent_page?(:controller=>'users',:action=>"show")如果我向routes.rb添加一个“匹配”命令,它工作正常,但如果资源已经创建,为什么我需要这样做呢?我错过了什么? 最佳答案 如果你只是想测试当前的Controller,你可以这样做:ifparams[:controller]=='u

ruby-on-rails - save_and_open_page 和 spork,spork 正在丢失测试套件/输出

当我用spork运行我的rspec测试时,每次我使用capybara的save_and_open_page时,spork都会丢失测试套件......或者可能不再输出任何东西......查看日志#=>withoutsave_and_open_page09:04:24-INFO-SporkserverforRSpec,Test::Unitsuccessfullystarted09:04:24-INFO-Guard::RSpecisrunning09:04:24-INFO-RunningallspecsRunningtestswithargs["--drb","-f","progress",

ruby - 为什么我不能在初始化中使用 attr_accessor?

我正在尝试在initialize中执行一个instance_eval,然后执行一个attr_accessor,并且我一直得到这个:``initialize':未定义的方法“attr_accessor”。为什么这不起作用?代码看起来像这样:classMyClassdefinitialize(*args)instance_eval"attr_accessor:#{sym}"endend 最佳答案 不能在实例上调用attr_accessor,因为attr_accessor没有定义为MyClass的实例方法。它仅适用于模块和类。我怀疑您想在

ruby-on-rails - Rails 的 ActiveRecord 序列化 :attr method gives "Missing Class or module error"

我试图在ActiveRecord模型中序列化一个简单的属性,而Rails2.3.4不喜欢它。classShopperserialize:tagsend>>a=Shopper.new=>>>a.tags=['aoeu','stnh']=>['aoeu','snth']>>a.save=>TypeError:classormodulerequired有人知道我错过了什么吗? 最佳答案 Arf...我以为我可以一次序列化两个属性,但事实并非如此:serialize:tags,:garments#thisiswrong第二个参数应该是序列化