草庐IT

内部空间

全部标签

ruby - 使用 YARD for Ruby 文档的内部评论?

我正在将我维护的ruby​​gem从RDoc切换到YARD文档。但是,代码中有一些关键注释只需要保留在代码中,不应出现在文档中。例如:###SomeClassdocumentationhere.#--#CRITICALcommentthatshouldbeinthecodebutnotinthedocumentation,#andmustbeatthisparticularspotinthecode.#++#moredocumentationthatfollowsthecriticalcommentblock,butthispart#shouldbeinthegenerateddocu

Ruby - 从对象内部调用 setter

这个问题在这里已经有了答案:WhydoRubysettersneed"self."qualificationwithintheclass?(3个答案)关闭7年前。我一直在研究PragmaticProgrammers的“ProgrammingRuby”一书,想知道是否可以在类中调用setter方法,而不是直接分配给实例变量。classBookInStockattr_reader:isbn,:pricedefinitialize(isbn,price)@isbn=isbn@price=Float(price)enddefprice_in_centsInteger(price*100+0.5

Ruby:是否有关键字可以从自身内部调用方法(类似于 super)?

我想知道:在Ruby中,有没有一种方法可以在不使用其名称的情况下从自身内部调用方法?如果该方法是通过某些元编程技术创建的,那么通过其名称调用它可能会难以阅读。即使对于通常定义的方法,如果您不确定它的好名字,或者如果它的名字很长,通过一些关键字(类似于super)从自身内部调用它可能会很方便。 最佳答案 您可以使用Kernel#__method__以Symbol形式返回当前方法的名称。与super不同,它不是关键字而是常规方法,因此您必须将它连同必需的参数一起传递给send方法才能调用该方法。这是__method__返回的内容:obj

ruby - 如何从定义相同名称的模块内部访问ruby中的顶级实体

在一个模块中,我有一个名为Process的类。moduleMProcess=Class.newProcess::wait(0)end这会引发NoMethodError。如何从模块内部访问顶级Process?如果不重命名我的类(class),这完全有可能吗? 最佳答案 ::Process.wait(0) 关于ruby-如何从定义相同名称的模块内部访问ruby中的顶级实体,我们在StackOverflow上找到一个类似的问题: https://stackoverf

ruby - 如何从 eval 内部返回?

我有一个代码需要在eval中使用。有时我需要退出评估代码,但我的尝试会导致错误。例如:#expectedtosee1,2and5;not3nor4;andnoerrorseval"puts1;puts2;return;puts3;puts4"#=>Error:unexpectedreturnputs5我尝试了return、end、exit、break,但都没有成功。exit不会引发错误,但我没有得到5。(注意:我知道eval是邪恶的,但在这种情况下我需要使用它。) 最佳答案 谢谢大家,但我找到了最适合我的问题的解决方案:lambda

ruby-on-rails - Rails 中带后备功能的动态命名空间 Controller

我对新的Rails应用程序有一个有点奇怪的要求。我需要构建一个应用程序,其中所有路由都在多个命名空间中定义(让我解释一下)。我想要一个应用程序,其中学校科目(数学、英语等)是namespace:%w[mathenglish].eachdo|subject|namespacesubject.to_symdoresources:studentsendend这很棒而且有效,但它需要我为每个主题创建一个命名空间StudentsController,这意味着如果我添加一个新主题,那么我需要创建一个新Controller。我想创建一个Base::StudentsController,如果Math:

ruby-on-rails - Rails 路由 : Nested, 成员、集合、命名空间、范围和可定制

我想了解更多关于Rails路线的信息。成员和收藏#Exampleresourceroutewithoptions:resources:productsdomemberdoget'short'post'toggle'endcollectiondoget'sold'endend命名空间和作用域#Exampleresourceroutewithinanamespace:namespace:admindoresources:productsendscope:admindoresources:productsend约束,Redirect_to#Exampleresourceroutewithop

ruby-on-rails - Ruby on Rails link_to 内部 ID

我如何使用link_to才能正常转到页面上的特定(html)ID如果我想转到我可以使用的页面上的“whatever_id”ClickHere但我想使用我的link_to"mypage",:controller=>"index"},:id=>"#whatever_id"%>辅助方法。有谁知道如何做到这一点?可能吗?rails2.3.4 最佳答案 link_to可以将anchor添加到URL。来自documentation,link_to"Commentwall",profile_path(@profile,:anchor=>"wall

ruby-on-rails - Rails 中 Controller 的嵌套命名空间

我重构了我的Rails应用程序,我为每个子资源创建了一个Controller,这是相应的命名空间。api/v1/app/controller/manager.rbapi/v1/app/controller/manager/user.rbapi/v1/app/controller/manager/controller.rbapi/v1/app/controller/admin.rbapi/v1/app/controller/user.rbapi/v1/app/controller/controller.rbmanager命名空间下的用户资源的类定义是这样的classApi::V1::Ma

ruby - MRI 内部结构 : detailed explanation of rb_id2str

在MRI中,似乎rb_id2str()当您调用Symbol#to_s时负责完成所有工作.我惊讶地发现这是一个极其神秘的函数,而我认为这是一个相当直接的操作。我正在寻找有关此功能的详细说明。作为引用,这里是1.9.3中源代码的链接:http://rxr.whitequark.org/mri/source/parse.y?v=1.9.3-p195#9950一些具体问题:什么是四大ifblock在做什么?if(idif(idif(st_lookup(global_symbols.id_str,id,&data))if(is_attrset_id(id))如果能大致了解if语句中每个代码块的作