jjzjj

create_method

全部标签

Ruby 类实例方法 def 初始化 : instance or class method?

让我们来看一个普通的ruby​​类:classPersonattr_accessor:namedefinitializename@name=nameendendbob=Person.new("bob")我的问题是初始化的性质。事情是这样的,new显然是一个类方法,但在我看来initialize是一个实例方法(不是类),它在类方法创建的实例上调用new被调用。我有这个权利吗?或者有人可以阐明一些新的观点吗?我做了一些谷歌搜索,但找不到任何清晰度。 最佳答案 当一个新对象被初始化时(也就是说,当你在一个类上调用new时)有效调用的是这个

ruby-on-rails - 运行 Heckle 时出错? `current_code' : undefined method `translate' for Ruby2Ruby

我正在尝试运行Heckle,但一直出现错误:>specspec/controllers/my_controller_spec.rb--heckleMyController!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!exception=hasathickskin.There'snothingtoheckle.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(Runsthroughseveralmu

ruby-on-rails - 旧的 Ruby 错误在我的 Ruby on Rails 应用程序中反复出现,与 Class.create 和 delayed_job 相关

这个错误已经有好几个月了,在这里:http://www.ruby-forum.com/topic/1094002其中显示代码更改的两个链接:https://github.com/godfat/ruby/commit/f4e0e8f781b05c767ad2472a43a4ed0727a75708https://github.com/godfat/ruby/commit/c7a6cf975d88828c2ed27d253f41c480f9b66ad6我有Ruby1.9.2和rvm。我会把这些更改粘贴到适当的文件中,但我不知道如何粘贴。这在几天前就奏效了。我不能像这样执行RubyonRai

ruby-on-rails - 之前的工厂女孩​​(:create) callback not creating associations

我有一个FactoryGirl工厂,它创建一个Order但before(:create)回调不会创建关联的工厂对象:父类classOrder子类classOrderLine工厂Factory:orderdo...ignoredonumber_or_order_lines1endbefore(:create)do|order,evaluator|FactoryGirl.create_list:order_line,evaluator.number_or_order_lines,order:orderendendFactory:order_linedoassociation:userass

ruby - 如何 to_enum( :method) receive its block here?

这段代码来self发现的一个示例,计算数组中等于其索引的元素数。但是怎么办?[4,1,2,0].to_enum(:count).each_with_index{|elem,index|elem==index}我无法仅通过链接来完成,而且链中的求值顺序令人困惑。我的理解是我们正在使用Enumerable#count的重载,如果给定一个block,它会计算产生真值的元素的数量。我看到each_with_index具有判断项目是否等于其索引的逻辑。我不明白的是each_with_index如何成为count的block参数,或者为什么each_with_index像被调用一样工作直接在[4,

Ruby 习语 : method call or else default

在Ruby中执行此操作的正确方法是什么?defcallOrElse(obj,method,default)ifobj.respond_to?(method)obj.__send__(method)elsedefaultendend 最佳答案 因为还没有提供答案:result=obj.methodrescuedefault与@slhck一样,当我知道obj很有可能不会响应方法时,我可能不会使用它,但这是一个选项。 关于Ruby习语:methodcallorelsedefault,我们在St

ruby - Sinatra 应用程序中的错误 "undefined local variable or method ` logger '"when using ` logger.info`

我有以下Sinatra1.2.1应用程序代码:#app.rbrequire'sinatra'get'/'dologger.info"COUCOU"'Helloworld!'end并使用ruby-rubygemsapp.rb启动服务器。当我转到http://localhost:4567时出现错误:NameErrorat/undefinedlocalvariableormethod`logger'for#file:app.rblocation:blockinline:4我是否需要添加或配置一些东西才能在Sinatra中启用日志记录?阅读SinatraREADME和文档,似乎默认情况下为Si

ruby - create_additions 选项在 JSON.parse 中有什么作用

我在ruby​​sJSON.parse文档中遇到了create_additions选项,但我似乎找不到任何关于它的文档。rdoc描述了选项:create_additions:Ifsettofalse,theParserdoesn'tcreateadditionsevenifamatchingclassand::create_idwasfound.Thisoptiondefaultstotrue.出于好奇,谁能解释一下这个选项的实际作用以及JSON.parse的这个“附加”功能到底是什么? 最佳答案 “添加”是对纯JSON的添加。如果

ruby-on-rails - 覆盖类和实例方法的 method_missing?

我正在尝试编写一个通用模块,以将用于动态方法创建的method_missing模式应用于我的某些Rails模型。这些模型既有类方法也有实例方法。虽然我可以相当直接地为任一类案例编写模块:moduleClassVersionextendActiveSupport::ConcernmoduleClassMethodsdefmethod_missing(meth,*args,&block)ifmeth.to_s=~/^(.+)_async$/Async::handle_asyncself,$1,*args,&blockelsesupermeth,*args,&blockendend#Logi

ruby-on-rails - 无法安装 gems,因为 "undefined method ` invoke_with_build_args' for nil :NilClass"

我在Rubyrails上,我正在安装RubyonRails。我正在尝试安装gems,但它没有发生,我不确定为什么以及如何修复它。$geminstallbundlerERROR:Loadingcommand:install(LoadError)dlopen(/Users/nthulanemakgato/.rbenv/versions/2.1.1/lib/ruby/2.1.0/x86_64-darwin13.0/openssl.bundle,9):Librarynotloaded:/usr/local/opt/openssl/lib/libssl.1.0.0.dylibReferenced