草庐IT

BOOST_PP_DEFINED

全部标签

Ruby 元类 : why three when defined singleton methods?

让我们计算MRI范围内的类别:defcount_classesObjectSpace.count_objects[:T_CLASS]endk=count_classes用类方法定义类:classAdefself.foonilendend然后运行:putscount_classes-k#=>3请解释一下,为什么是三个? 最佳答案 查看MRI代码,每次你创建一个Class时,在Ruby中它是Class类型的对象,ruby会自动为这个新类创建“元类”类,这是另一个单例类型的Class对象。C函数调用(class.c)是:rb_define

ruby - main :Object (NoMethodError) though method is defined 的未定义方法

我已经使用以下代码片段定义了一个脚本:check_paramsparamdefcheck_params(param)#somecodeend当我运行它时,我得到了undefinedmethod`check_params'formain:Object(NoMethodError) 最佳答案 Ruby期望方法在你调用它之前被声明,尝试在你调用方法之前移动你的方法定义:defcheck_params(param)#somecodeendcheck_paramsparam 关于ruby-main

Ruby "defined?"运算符工作错误?

所以,我们有代码:classFoodefbarputs"Beforeexistent:#{(defined?some_variable)}"puts"Beforenot_existent:#{(defined?nonexistent_variable)}"raise"error"some_variable=42rescueputs"exception"ensureputs"Ensureexistent:#{(defined?some_variable)}"puts"Ensurenot_existent:#{(defined?nonexistent_variable)}"endend然后

ruby - pg gem : 'Warning: no type cast defined for type "numeric"'

我在从pggem中获取输入结果时遇到问题。require'pg'require_relative'spec/fixtures/database'client=PG.connect(DB[:pg])client.type_map_for_queries=PG::BasicTypeMapForQueries.new(client)client.type_map_for_results=PG::BasicTypeMapForResults.new(client)client.exec(%|select*fromtestme;|)do|query|query.each{|r|putsr.ins

ruby-on-rails - vim/macvim : locate where a method/symbol is defined

我的大部分Ruby+RubyonRails开发都使用macvim/vim。目前是否有一种方法可以跳转到项目中定义方法的位置,即使它与调用它的位置不在同一个文件中?与语言无关的方式或特定于Ruby/Rails的方式均可。 最佳答案 我推荐使用ctags插件,BryanLiles制作了一个很好的截屏视频,介绍如何在Rails开发中使用它。http://smartic.us/2009/04/05/using-ctags-in-vim/来自他的页面:不那么完整的备忘单:^]–查找标签^T——倒退:tags–显示你去过的地方:tag–进入你的

ruby-on-rails - OmniAuth Railscast 教程中的 DangerousAttributeError : create is defined by ActiveRecord

我看过ActiveRecord::DangerousAttributeError和SO上的其他类似线程,但它们没有解决相同的问题。我正在学习omniauth教程:http://railscasts.com/episodes/235-omniauth-part-1?view=asciicast我能够通过Twitter的oauth进行身份验证并返回用户的数据(auth)。问题是由于此错误消息,我无法在数据库(sqlite3)中创建/保存它。错误:ActiveRecord::DangerousAttributeErrorinAuthenticationsController#createcr

ruby-on-rails - 如何避免 Rspec 共享示例 'previously defined' 警告?

我正在尝试学习如何使用Rspec的共享示例功能,但在运行测试时收到警告:WARNING:Sharedexamplegroup'requiredattributes'hasbeenpreviouslydefinedat:/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1...andyouarenowdefiningitat:/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1Thenewdefinitio

c - 命令行错误消息的 "POSIX-defined format"是什么?哪个标准?

在ruby​​-doc.org上page我发现了以下关于命令行选项/参数解析(getopt库)的内容:ReturntheappropriateerrormessageinPOSIX-definedformat.Ifnoerrorhasoccurred,returnsnil.命令行错误消息的POSIX定义格式是什么?它是哪个POSIX标准?编辑:我必须澄清一下,我对标准/推荐的错误消息很感兴趣解析命令行参数/选项。在下面的链接(答案)中,我发现只提到了getopt的这种错误格式:"%s:illegaloption--%c\n",,"%s:optionrequiresanargument-

ruby - defined?(super) 检查的奇怪行为

最近我发现defined?运算符有一些奇怪的行为,用于检查super关键字是否可以在当前上下文中使用。通常它工作正常,但是当我尝试组合定义的时?super用一点元编程检查,它给了我意想不到的结果。显示比描述更容易,所以这里是一个提炼的例子来说明问题:classA;defself.def_f!;singleton_class.send(:define_method,:f){defined?super}endendclassAA(A和AA类都有.def_f!类方法)A.def_f!A.f#=>nilAA.f#=>nil(A.f没有super和AA.f调度到A.f,所以到目前为止一切正常,但

ruby - `defined?` 和 `unless` 未按预期工作

我期待以下片段:var="NotEmpty"unlessdefined?varvar#=>nil返回"NotEmpty",但我得到了nil。是否了解为什么会发生这种情况? 最佳答案 这是Ruby中仅有的几个我称之为真正WTF的时刻之一。你必须使用unlessdefined?varvar=:valueend使用后缀语法,解释器将在内部nil-ify值,以便它可以推断变量,从而在检查完成之前定义它:#Doesn'tprintanythingunlessdefined?(foo)and(p(foo)ortrue)foo=:valueend