jjzjj

Include_once

全部标签

ruby - Ruby 中用于 "String#include?"的算法

有谁能指出包含的算法是什么?Ruby中的方法?例如"helloworld".include?("hello") 最佳答案 正如emboss在他的回答中所述,String#include调用rb_str_index。此函数依次调用rb_memsearch,它实现了Rabin-Karpstringsearchalgorithm,根据thispost在ruby-forum.com上。 关于ruby-Ruby中用于"String#include?"的算法,我们在StackOverflow上找到一

ruby - "base.send :include, InstanceMethods"---> 这是做什么的?

我正在查看一个模块X,它包含两个名为“InstanceMethods”和“ClassMethods”的模块。模块X中的最后一个定义是这样的:defself.included(base)base.send:include,InstanceMethodsbase.send:extend,ClassMethodsend这是做什么的? 最佳答案 included在一个模块被包含到另一个模块或类中时被调用。在这种情况下,它将尝试调用base的include方法来从InstanceMethods中获取模块方法、变量和常量添加到base然后将尝试

ruby - 在 ruby​​ 上,为什么 include 是私有(private)的而 extend 是公共(public)的?

在ruby上,为什么include是private,而Object#extend是public? 最佳答案 Object#extend必须是公开的,否则您将无法使用它。毕竟,它的目的是将模块混合到对象中,因此您通常会像obj.extend(Foo)那样调用它,这对于私有(private)方法是不可能的。Module#include通常只在模块体内使用,如下所示:classBarincludeFooend即它通常在没有接收者的情况下被调用,所以它不必公开。当然,它也必须是私有(private)的。我猜它之所以是私有(private)的

ruby-on-rails - 对于 Ruby, "gem install"是否使用 "--include-dependencies"...只是文档有点过时了?

使用RubyonRails,如果我做一个gemhelpinstall它的一部分说:-y,--include-dependenciesUnconditionallyinstalltherequireddependentgems[...]Defaults:--both--version'>=0'--rdoc--ri--no-force--no-test--install-dirc:/ruby/lib/ruby/gems/1.8但是如果我做一个geminstall--include-dependenciesmysql一行说:INFO:`geminstall-y`isnowdefaultand

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 - 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-on-rails - 是否可以在单个 "include"语句中包含多个模块?

是否有更短的方法来执行以下操作?classMyClassincludeMyModule1includeMyModule2includeMyModule3end 最佳答案 尝试跟随classMyClassincludeMyModule3,MyModule2,MyModule1end编辑:颠倒顺序 关于ruby-on-rails-是否可以在单个"include"语句中包含多个模块?,我们在StackOverflow上找到一个类似的问题: https://stack

ruby - Ruby 中的 "include module"和 "extend module"有什么区别?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:WhatisthedifferencebetweenincludeandextendinRuby?给定:modulemy_moduledeffoo...endend问题一有什么区别:classAincludemy_moduleend和classAextendmy_moduleend问题二将foo视为实例方法还是类方法?换句话说,这是否等同于:classAdeffoo...endend或:classAdefself.foo...endend?