草庐IT

fs_class_fs_stats

全部标签

ruby - class << self 是什么意思?

关于:classTestclass我脑海中浮现出如下画面:因为在Ruby中一切都是对象,类本身就是类Class的对象。.调用class你打开ClassTest内部的定义并注入(inject)一些实例方法。自Test是Class的实例,您可以像在对象上调用实例方法一样调用这些方法:Test.hi.以下是有助于可视化我之前句子的伪代码:classClassdefhiputs“Hithere”endendTest=Class.new(classTestend)Test.hi我做对了吗? 最佳答案 假设我们有一个对象obj类A.此时,obj

ruby - 在 define_method 中调用 super 时没有父类(super class)方法

当我重写一个已经存在的方法时,为什么会出现以下错误talk:super:nosuperclassmethodtalk(NoMethodError)?如何修复此代码以调用super方法?这是我正在使用的示例代码classFoodeftalk(who,what,where)p"#{who}is#{what}at#{where}"endendFoo.new.talk("monster","jumping","home")classFoodefine_method(:talk)do|*params|super(*params)endendFoo.new.talk("monster","jump

ruby - Redmine邮箱配置: "undefined method ` email_delivery =' for ActionMailer::Base:Class"

我在第一次安装Redmine后为Redmine配置电子邮件通知。我创建了/etc/redmine/default/email.yml并添加了:#Outgoingemailsettingsproduction:email_delivery:delivery_method::smtpsmtp_settings:address:smtp.example.comport:25domain:example.comauthentication::loginuser_name:examplepassword:example访问时http://redmine/我遇到一个应用程序异常:“ActionMa

ruby-on-rails - rails/Prawn : how do I use rails helpers inside a Prawn class?

我正在尝试在prawn类中使用rails3.2助手,但rails抛出:undefinedmethod`number_with_precision'for#Prawn类classQuotePdfControllerdefshow@quote=current_user.company.quotes.where(:id=>params[:id]).firsthead:unauthorizedandreturnunless@quoterespond_with@quote,:layout=>!params[:_pjax]do|format|format.pdfdosend_dataQuotePd

ruby-on-rails - PinsController#index : declare the formats your controller responds to in the class level 中的运行时错误

在我的Rails应用程序上工作时,我在终端中使用以下命令创建了一个“Pins”脚手架:railsgeneratescaffoldPinsdescription:string--skip-stylesheets这会在我的应用程序中创建脚手架,然后我运行:rakedb:migrate一切顺利。我没有更改任何生成的页面,但是当我最终尝试访问localhost:3000上的新脚手架时,出现以下错误:RuntimeErrorinPinsController#indexInordertouserespond_with,firstyouneedtodeclaretheformatsyourcontr

ruby-on-rails - rails : how to extend a gem's ActiveRecord child class?

我在扩展一个在gem中定义并且是ActiveRecord::Base的子类的类时遇到问题。我唯一想扩展这个类的是:有很多:promos然而,扩展倾向于排除原始类。我得到的错误:PGError:ERROR:relation"sites"doesnotexistLINE4:WHEREa.attrelid='"sites"'::regclass^:SELECTa.attname,format_type(a.atttypid,a.atttypmod),d.adsrc,a.attnotnullFROMpg_attributeaLEFTJOINpg_attrdefdONa.attrelid=d.a

ruby - "const_missing"定义中缺少常量和 "class << self"

在定义const_missing时,我对Ruby的行为感到非常困惑和class中的其他类方法定义而不是使用defself.foo句法。我正在尝试做这样的事情:classFooclass我主要使用class定义类方法的语法。但是,它没有按预期工作。const_missing永远不会被调用。以上结果导致NameError。像这样定义这两种方法按预期工作:defself.fooputsMISSINGenddefself.const_missing(name)puts"#{name}missing"end我认为classsyntax只是定义类方法的另一种方式,但完全等同于defself.foo

Ruby 2.4.1 Dir.children( dirname ) 为 Dir :Class"返回 "undefined method ` children'

我是Ruby的新手,正在尝试学习它。我使用的是最新的Ruby版本(2.4.1)和交互式RubyShell。我在Dir类中遇到过children方法。我试过这个例子fromthedocumentation:Dir.children("testdir")#=>["config.h","main.rb"]但它似乎不起作用,因为我收到以下消息:undefinedmethod`children'forDir:Class我错过了什么? 最佳答案 这似乎是某种文档困惑。Dir.children方法是在Feature#11302中引入的进入Ruby

ruby - 基本元编程 : extending an existing class using a module?

我希望我的模块的一部分能够扩展String类。这行不通moduleMyModuleclassStringdefexclaimselfNoMethodError但这样做moduleMyModuledefexclaimself"thisisastring!!!!!"我不希望MyModule的所有其他功能都被String束缚。在最高级别再次包含它似乎很丑陋。当然有更简洁的方法来做到这一点? 最佳答案 exclaim第一个示例中的方法是在一个名为MyModule::String的类中定义的,与标准无关String类(class)。在您的模块

ruby - 为什么 self.class === MyClass 返回 false,而 self.class == MyClass 返回 true?

我正在使用Ruby的case语法来设置一些基于self.class的简单逻辑,如下所示:caseself.classwhenFirstClassdostuff....whenSecondClassdootherstuff...end我很快意识到这总是返回nil。经过仔细调查,我发现case使用===而不是==检查是否相等。在我的终端中运行self.class==FirstClass时,我按预期得到true,但是self.class===FirstClass返回假的。查看ruby​​文档,我找到了followingexplanation===:CaseEquality–ForclassO