草庐IT

ruby 混合 : extend and include

我已经阅读了一些关于Ruby的mixin方法、extend和include的文章,但我仍然不太确定其行为。我知道extend会将给定模块的实例方法作为单例方法添加到执行扩展的模块中,而include基本上会附加模块的内容(方法,常量,变量)到执行包含的那个,有效地在接收器中定义它们。然而,经过一些修补,试图了解行为将如何表现出来后,我有几个问题。这是我的测试设置:moduleBazdefblorgputs'blorg'endendmoduleBarincludeBazdefblahputs'blah'endendmoduleFooextendBarendclassBaconextend

ruby - Ruby 的 include 的对立面是什么?

如果我有一个包含多个包含模块的类实例,我可以动态取消包含特定模块(以便替换它)吗?谢谢。 最佳答案 没有。您不能在Ruby语言中取消包含混入。不过,在某些Ruby实现上,您可以通过用C或Java编写特定于实现的扩展来实现(对于Rubinius,甚至是Ruby)。 关于ruby-Ruby的include的对立面是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/2088211/

ruby - Ruby 中的 'include' 和 'prepend' 有什么区别?

来自ModuleModule#append_features(mod)→mod=>Whenthismoduleisincludedinanother,Rubycallsappend_featuresinthismodule,passingitthereceivingmoduleinmod.Ruby’sdefaultimplementationistoaddtheconstants,methods,andmodulevariablesofthismoduletomodifthismodulehasnotalreadybeenaddedtomodoroneofitsancestors.Mo

ruby-on-rails - 无法安装 Nokogiri gem

我正在开发一个允许图像附件到每个用户帐户的Rails应用程序。我正在使用回形针和亚马逊网络服务:gem'paperclip'gem'aws-sdk'当我运行bundleinstall时,我收到这条消息:extconf失败,退出代码1Gemfileswillremaininstalledin/usr/local/rvm/gems/ruby-2.1.2/gems/nokogiri-1.6.5forinspection.Resultsloggedto/usr/local/rvm/gems/ruby-2.1.2/extensions/x86_64-darwin-13/2.1.0-static/

ruby - 确定一个字符串是否包含一组字符串中的任何一个

我正在使用Mechanize与一些网页进行交互,并且我正在尝试确定给定的表单提交是否导致了错误。现在我正在这样做:agent.page.body.include?("I'manerrormessage!")但我刚刚发现另一个错误信息。因为我不想这样做:agent.page.body.include?("I'manerrormessage!")||agent.page.body.include?("Anothererrormessage")如何确定页面正文是否包含任一个错误消息? 最佳答案 error_messages.any?{|m

ruby - Ruby 中圆括号的惯用用法

array.include?'foo'orarray.include?'bar'是语法错误(意外的keyword_or)。括号解决了这个问题,但由于我是Ruby的新手,我不知道以下哪一项被认为更符合惯用语:选项1array.include?('foo')orarray.include?('bar')选项2(array.include?'foo')or(array.include?'bar')这是否归结为个人偏好,还是一种方法被认为更“正确”? 最佳答案 我建议您看一下community-drivenRubycodingstylegu

ruby - 在字符串数组中查找字符串的最快方法

脚本必须验证一个预定义的IP是否存在于大量IP中。目前我编写的函数是这样的(说“ips”是我的IP数组,“ip”是预定义的ip)ips.eachdo|existsip|ifip==existsipputs"ipexists"return1endendputs"ipdoesn'texist"returnnil有没有更快的方法来做同样的事情?编辑:我可能表达错误了。我可以做array.include吗?但我想知道的是:array.include是什么?给我最快结果的方法? 最佳答案 您可以使用Set.它是在Hash之上实现的,对于大数据

ruby-on-rails - ruby - 在 case 语句中使用 include 方法

我正在使用这样的东西:caserefererwhen(referer.include?"some_string")redirect_link=edit_product_pathwhen(referer.include?"some_other_string")redirect_link=other_product_pathend不幸的是,即使字符串some_string出现在变量referer中,这也会返回nil。这是我在Ruby控制台中尝试的:ruby-1.8.7-p334:006>jasdeep="RAILS"ruby-1.8.7-p334:026>casejasdeepruby-1

ruby - 无法编译 ruby​​ 1.9.3

当我运行时rvminstall1.9.3--with-gcc=clang在MacOSXLion上,ruby无法编译,我在(.rvm/log/ruby-1.9.3-p374/make.log)中收到以下错误[2013-01-2916:31:05]makeCC=clangLD=ldLDSHARED=clang-dynamic-bundleCFLAGS=-O3-ggdb-Wall-Wextra-Wno-unused-parameter-Wno-parentheses-Wno-long-long-Wno-missing-field-initializers-Werror=pointer-ari

ruby - Mac Rmagick 无法与 Xcode 4.2 一起安装

我刚买了一台新的macbookpro,正在尝试设置我的开发环境。我从应用商店下载了xcode4.2并安装了它,之后我安装了自制软件和RVM。ImageMagick、readline、ruby1.9.3-head都完美安装,直到我运行bundleupdate试图安装rmagick。经过很长时间的调查,我得出的结论是它找不到libgomp。geminstallrmagick的输出是:$geminstallrmagickBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrmagick:ERROR:Faile