jjzjj

first-instance_method

全部标签

Ruby 单例方法 (class_eval, define_method) vs (instance_eval, define_method)

Ruby中的元编程很棒,因为我经常使用它来模拟基于原型(prototype)的编程,并快速编写一些问题的原型(prototype)解决方案来测试它们的可行性。所以我想知道下面这段代码是否有本质区别:(class和(class代码的两个版本都定义了一个单例方法,我还没有遇到任何迫使我选择(instance_eval,define_method)组合而不是(class_eval,define_method)组合来定义单例方法,我想知道两者之间是否存在一些本质区别。 最佳答案 define_method没有区别。但是当您使用def时会有所

ruby - 了解类方法的 method_added

我想在将实例和类方法添加到某个类时施展魔法。因此我尝试了以下方法:moduleMagicdefself.included(base)base.extendClassMethodsendmoduleClassMethodsdefmethod_added(name)puts"classmethod'#{name}'added"enddefsome_class_methodputs"someclassmethod"endendendclassFooincludeMagicdefself.method_added(name)puts"instancemethod#{name}added"end

ruby-on-rails - ActiveRecord::Base 上的 alias_method 导致 NameError

我有一个直接从ActiveResource::Base继承的模型,我正在尝试为记录表中的大部分列运行alias_method,但结果是一个NameError:NameError:undefinedmethodaddress_line_1'forclassLeadImport::Base'但我可以访问属性:LeadImport::Base.new.address_line_1#=>nil(noterror)我的类(class)有一个名为address_line_1的表列,所以我没有看到问题。classLeadImport::Base规范:Ruby1.8.7、Rails2.3.8

ruby - 加载网页时 : "undefined method ` request_uri' for #"

我正在尝试使用Ruby通过HTTP加载网页并检查其状态代码是什么。我的代码如下所示:require"net/http"@r=Net::HTTP.get_response(URI.parse(myURL))return@r.code但是,对于某些URL(主要是指向奇怪的东西,例如不会给出正确响应的Web计数器),我得到了一个undefinedmethodrequest_urifor#异常。我已经将它追溯到http.rb的第380行(我正在运行Ruby1.8),它说:defHTTP.get_response(uri_or_host,path=nil,port=nil,&block)ifpa

ruby - 使用 WWW :Mechanize to download a file to disk without loading it all in memory first

我正在使用Mechanize来简化某些文件的下载。目前我的脚本使用以下行来实际下载文件...agent.get('http://example.com/foo').save_as'a_file_name'然而,这会将完整的文件下载到内存中,然后再将其转储到磁盘。你如何绕过这种行为,直接下载到磁盘?如果我需要使用WWW:Mechanize以外的东西,那么我将如何使用WWW:Mechanize的cookies呢? 最佳答案 您真正想要的是Mechanize::Downloadhttp://mechanize.rubyforge.org/

ruby-on-rails - ActiveAdmin 错误 : no superclass method `buttons'

我从Rails开始(我也是Ruby的新手-来自Python-)并且我目前正在尝试为Rails3.2.3(Ruby1.9.3)设置ActiveAdmin。我正在关注thisguide但我无法正常运行它。当我运行railss命令访问localhost:3000/admin我得到NoMethodErrorinActive_admin/devise/sessions#newShowing/home/lex/.rvm/gems/ruby-1.9.3-p125/gems/activeadmin-0.4.3/app/views/active_admin/devise/sessions/new.htm

ruby - 全新安装 OSX 10.9.1 在尝试安装 gems 时返回 "undefined method ` path2class'"

我刚刚使用Homebrew和RVM安装了一个干净的Mavericks安装。brewdoctor和rvmrequirements都返回“allgood”,但是,当我在我的项目目录中运行bundleinstall时,我的大多数gem安装都很好,但少数安装失败并出现相同的以下错误:Bundler::GemspecError:Couldnotreadgemat/Users/NK/.rvm/gems/ruby-2.0.0-p353/cache/eventmachine-1.0.3.gem.Itmaybecorrupted.Anerroroccurredwhileinstallingeventma

ruby - 导轨/RSpec : How to test #initialize method?

如何使用RSpec指定#initialize行为?例如这里:generator.rbclassGeneratorattr_accessor:seeddefinitialize(seed=nil)@seed=seed||pick_seedenddefpick_seedTime.now.to_iendendgenerator_spec.rbrequire'generator'describeGeneratorit"calls'pick_seed'methodunlessseedspecified"doendend我想设置从#initialize方法调用的pick_seed方法的期望值。

ruby - 资源编译在服务器环境中崩溃,出现 : "NoMethodError: undefined method ` [ ]' for nil:NilClass"

为了这个我一直在努力。我一直与Assets管道关系不好,它总是给我带来麻烦..今天又是..当我尝试在本地编译我的Assets时,一切都很好:$RAILS_ENV=productionrakeassets:precompile--trace但是当我使用Capistrano部署时:cd/var/www/xxx/releases/20140717164232&&(RAILS_ENV=productionbundleexecrakeassets:precompile)它因以下错误而崩溃:rakeaborted!NoMethodError:undefinedmethod`[]'fornil:Ni

ruby - 'yield self' 和 instance_eval 一样吗?

如果用instance_eval:定义Foo有什么不同吗?..classFoodefinitialize(&block)instance_eval(&block)ifblock_given?endend。..或者使用“yieldself”:classFoodefinitializeyieldselfifblock_given?endend无论哪种情况,您都可以这样做:x=Foo.new{deffoo;'foo';end}x.foo所以“yieldself”意味着Foo.new之后的block总是在Foo类的上下文中求值。这是正确的吗? 最佳答案