草庐IT

convenience-methods

全部标签

ruby - 定义 "method_called".. 如何制作一个钩子(Hook)方法,每次调用类的任何函数时都会调用该方法?

我想制作一个钩子(Hook)方法,每次调用一个类的任何函数时都会调用它。我试过method_added,但是它只在类定义的时候执行一次,classBasedefself.method_added(name)p"#{name.to_s.capitalize}Method'sbeencalled!!"enddefap"acalled."enddefbp"bcalled."endendt1=Base.newt1.at1.bt1.at1.bOutput:"AMethod'sbeencalled!!""BMethod'sbeencalled!!""acalled.""bcalled.""acal

ruby - 在 ruby​​ 中,self.method 和类 << self 中的方法有什么区别

classFoodefself.one;1endclass["two","one"]有人告诉我上述方法“一”和“二”在概念上是不同的,但我不明白是怎么回事。它们都是单例方法-在概念和应用上有什么区别? 最佳答案 在应用上,没有区别。在概念上,区别是微妙的,但在第一种情况下,您是在当前上下文中操作,并在另一个类实例上定义一个方法(实际上是其Eigenclass中的实例方法),而在第二种情况下,您正在进入其他类实例的元类(“Eigenclass”)的上下文,然后定义一个实例方法。编辑:我应该补充说选择class的原因在某些情况下是...

ruby - 这是 Method#to_proc 中的错误吗? ( ruby 1.8.7)

给定以下采用一个参数的方法:deffoo(arg);parg;end我可以用一个空数组来调用它:foo([])#prints[]我也可以将它保存为一个Method对象并用一个空数组调用that,结果相同:method(:foo).call([])#prints[]但是,如果我将Method对象转换为Proc并使用空数组调用that,我会得到一个ArgumentError:method(:foo).to_proc.call([])#ArgumentError:wrongnumberofarguments(0for1)#from(irb):4:in`foo'#from(irb):4:in`

ruby-on-rails - Rails - 测试 fixture 错误 NoMethodError : undefined method `type' for nil:NilClass

我在运行使用具有模型之间关联的固定装置的测试时遇到问题。这是我一运行raketest就得到的错误:ERROR["test_truth",SevenPortfolioTest,0.005154775]test_truth#SevenPortfolioTest(0.01s)NoMethodError:NoMethodError:undefinedmethod`type'fornil:NilClassERROR["test_should_destroy_item_video",SevenPortfolio::ItemVideosControllerTest,0.008887804]test_

ruby - alias_method 和 class_methods 不混合?

我一直在尝试修补全局缓存模块,但我不明白为什么它不起作用。有人有什么建议吗?这是错误:NameError:undefinedmethod`get'formodule`Cache'from(irb):21:in`alias_method'...由此代码生成:moduleCachedefself.getputs"original"endendmoduleCachedefself.get_modifiedputs"Newget"endenddefpeek_a_booCache.module_evaldo#make:get_not_modifiedalias_method:get_not_mo

ruby-on-rails - Rails 茧 gem : Undefined Method 'new_record?' on link_to_remove_association with Wicked

我一直在努力弄清楚一些代码。我有一个表格,我正在尝试使用Wicked和茧gem。一切正常,包括link_to_add_association功能。正如Cocoon所建议的那样,我正在为关联的表单字段呈现部分内容,并且除了link_to_remove_association函数之外,一切似乎都正常工作。它返回以下错误:nil:NilClass的未定义方法new_record?这是我抛出错误的部分:这是调用部分的View:location%>这是调用View的Controller操作:classUserStepsController如果有帮助,这里是cocoon中抛出错误的函数:defli

ruby-on-rails - Rails 的 ActiveRecord 序列化 :attr method gives "Missing Class or module error"

我试图在ActiveRecord模型中序列化一个简单的属性,而Rails2.3.4不喜欢它。classShopperserialize:tagsend>>a=Shopper.new=>>>a.tags=['aoeu','stnh']=>['aoeu','snth']>>a.save=>TypeError:classormodulerequired有人知道我错过了什么吗? 最佳答案 Arf...我以为我可以一次序列化两个属性,但事实并非如此:serialize:tags,:garments#thisiswrong第二个参数应该是序列化

ruby-on-rails - self.send(method, =, value) 不起作用

我正在尝试创建一个Ruby类,其中initialize方法接受选项的散列。然后,我将这些选项作为类的attr_accessor。现在,我可以做类似的事情classUserattr_accessor:name,:email,:phonedefinitialize(options)self.name=options[:name]self.email=options[:email]self.phone=options[:phone]endendUser.new(:name=>'SomeName',:email=>'some-name@some-company.com',:phone=>435

ruby-on-rails - class_methods 在关注中做了什么?

我正在阅读一些使用Rails4中关注点的代码。我看了一些文章说,如果我们想包含类方法使用模块ClassMethods,但我阅读的代码使用类似:class_methodsdodef****endend 最佳答案 ActiveSupport::Concern为模块混合的常见Ruby模式提供语法糖。当您使用modulesasmixins时你不能像在类中那样只使用self来声明类方法:moduleFoodefself.bar"HelloWorld"enddefinstance_method"HelloWorld"endendclassBaz

ruby - 试图理解 self.method_name 与 Classname.method_name 在 Ruby 中的使用

我试图了解何时使用self.method_name与何时使用Classname.method_name。在下面的示例中,为什么“before_create”需要引用“User.hash_password”而不是“self.hash_password”或只是“hash_password”?由于我们已经在User类中,我认为before_create方法会“知道”“hash_password”是它自己的类的成员,不需要任何特殊语法来引用它。require'digest/sha1'classUser["name=?andhashed_password=?",name,hashed_passw