草庐IT

additional-methods-and-properties

全部标签

ruby-on-rails - Ruby on Rails : alias_method_chain, 究竟是做什么的?

我尝试阅读了各种博客文章,这些文章试图解释alias_method_chain以及使用和不使用它的原因。我特别注意:http://weblog.rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain和http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/我仍然看不到alias_method_chain有任何实际用途。谁能解释一下。1-它还在使用吗?2-你什么时候会使用alias_method_chain以及为什么?

ruby-on-rails - 为什么我在安装 PaperClip 时得到一个 "undefined method for ` has_attached_file`?

我刚刚安装了Paperclip插件,但收到以下错误消息,但我不确定原因:NoMethodError(undefinedmethod`has_attached_file'for#):/Users/bgadoci/.gem/ruby/1.8/gems/will_paginate-2.3.12/lib/will_paginate/finder.rb:170:in`method_missing'app/models/post.rb:2app/controllers/posts_controller.rb:50:in`show'它引用了will_paginategem。据我所知,我的PostsC

ruby - class << self vs self.method with Ruby : what's better?

这RubyStyleGuide告诉我们最好使用self.method_name而不是classmethod_name。但是为什么?classTestClass#badclass是否存在性能问题? 最佳答案 class善于将所有类方法放在同一个block中。如果在defself.method中添加方法form则不能保证(除了惯例和一厢情愿的想法)不会有额外的类方法隐藏在文件的后面。defself.method擅长显式声明方法是类方法,而使用class你必须自己去找容器。这些中哪一个对您来说更重要是一个主观决定,并且还取决于诸如有多少其

Ruby:define_method 与 def

作为一项编程练习,我编写了一个Ruby片段,它创建了一个类,实例化了该类中的两个对象,猴子修补了一个对象,并依靠method_missing猴子修补了另一个对象。这是交易。这按预期工作:classMonkeydefchatterputs"Iamachatteringmonkey!"enddefmethod_missing(m)puts"No#{m},soI'llmakeone..."defscreechputs"Thisisthenewscreech."endendendm1=Monkey.newm2=Monkey.newm1.chatterm2.chatterdefm1.screec

ruby-on-rails - 不兼容的字符编码 : ASCII-8BIT and UTF-8

我使用Ruby1.9.2和Rails3.0.5我有以下错误:incompatiblecharacterencodings:ASCII-8BITandUTF-8我认为这与数据库无关。错误发生在View中的这一行(只是一个divhaml调用):#content全栈:ActionView::Template::Error(incompatiblecharacterencodings:ASCII-8BITandUTF-8):21:-flash.eachdo|name,msg|22:=content_tag:div,msg,:id=>"flash_#{name}"23:%div.clear24:

ruby-on-rails - rails : Validating min and max length of a string but allowing it to be blank

我有一个要验证的字段。我希望该字段能够留空,但如果用户正在输入数据,我希望它采用某种格式。目前我在模型中使用以下验证,但这不允许用户将其留空:validates_length_of:foo,:maximum=>5validates_length_of:foo,:minimum=>5如何编写此代码以实现我的目标? 最佳答案 你也可以使用这种格式:validates:foo,length:{minimum:5,maximum:5},allow_blank:true或者因为您的最小值和最大值相同,以下也将起作用:validates:foo

ruby - `raise "foo "` and ` raise Exception.new ("foo")` 有什么区别?

在技术、哲学、概念或其他方面有什么区别raise"foo"和raiseException.new("foo")? 最佳答案 从技术上讲,第一个引发RuntimeError,消息设置为"foo",第二个引发异常,消息设置为"foo".实际上,使用前者和使用后者之间存在显着差异。简单地说,您可能想要一个RuntimeError不是Exception.没有参数的救援block将捕获RuntimeErrors,但不会捕获Exception秒。所以如果你提出Exception在您的代码中,此代码不会捕获它:beginrescueend为了ca

ruby - 如何使用 define_method 创建类方法?

如果您尝试以元编程方式创建类方法,这将很有用:defself.create_methods(method_name)#Tocreateinstancemethods:define_methodmethod_namedo...end#Tocreateclassmethodsthatrefertotheargsoncreate_methods:???end我要遵循的答案... 最佳答案 我认为在Ruby1.9中你可以这样做:classAdefine_singleton_method:loudlydo|message|putsmessag

ruby - 你能为 map 提供参数吗(&:method) syntax in Ruby?

您可能熟悉以下Ruby速记(a是一个数组):a.map(&:method)例如,在irb中尝试以下操作:>>a=[:a,'a',1,1.0]=>[:a,"a",1,1.0]>>a.map(&:class)=>[Symbol,String,Fixnum,Float]语法a.map(&:class)是a.map{|x|的简写x.class.在“Whatdoesmap(&:name)meaninRuby?”中阅读有关此语法的更多信息。通过语法&:class,您正在为每个数组元素调用方法class。我的问题是:您可以为方法调用提供参数吗?如果是这样,怎么做到的?比如下面的语法你怎么转换a=[1

ruby - 如何解决 "You need to have Ruby and Sass installed and in your PATH for this task to work"警告?

我正在为工作设置一台新Mac。我已经在全局范围内安装了Grunt&GruntCLI。然后我在项目文件夹中执行了npminstall以安装所有依赖项。到目前为止没有问题,但是当我尝试运行sass:dist任务时,我收到了这个警告:Warning:YouneedtohaveRubyandSassinstalledandinyourPATHforthistasktowork.Moreinfo:https://github.com/gruntjs/grunt-contrib-sassUse--forcetocontinue.据我了解,我需要在更全局的级别上安装Ruby和Sass才能运行此任务。