草庐IT

instance-initializers

全部标签

ruby - 在 Ruby 中, 'new' 和 'initialize' 之间的关系是什么?初始化时如何返回nil?

我想要的是:obj=Foo.new(0)#=>nilorfalse这行不通:classFoodefinitialize(val)returnnilifval==0endend我知道在C/C++/Java/C#中,我们不能在构造函数中返回值。但我想知道在Ruby中是否可行。 最佳答案 InRuby,what'stherelationshipbetween'new'and'initialize'?new通常调用initialize。new的默认实现类似于:classClassdefnew(*args,&block)obj=allocat

ruby - 如何理解class_eval()和instance_eval()的区别?

Foo=Class.newFoo.class_evaldodefclass_bar"class_bar"endendFoo.instance_evaldodefinstance_bar"instance_bar"endendFoo.class_bar#=>undefinedmethod‘class_bar’forFoo:ClassFoo.new.class_bar#=>"class_bar"Foo.instance_bar#=>"instance_bar"Foo.new.instance_bar#=>undefinedmethod‘instance_bar’for#仅根据方法的名称,我

ruby-on-rails - rails : Should partials be aware of instance variables?

例如,RyanBates的nifty_scaffolding就是这样做的编辑.html.erb'form'%>new.html.erb'form'%>_form.html.erb那种隐藏的状态让我觉得不舒服,所以我通常喜欢这样做编辑.html.erb'form',:locals=>{:object=>@my_object}%>_form.html.erb那么哪个更好:a)让部分访问实例变量或b)传递部分它需要的所有变量?最近我一直选择b),但我确实遇到了一些问题:some_action.html.erb'partial',:locals=>{:son=>a_son}%>_partial

ruby - 烦人的 "warning: already initialized constant"消息的解决方案

今天我偶然发现了一个与Ruby常量有关的棘手问题。在我们的团队中,有人创建了一个模块,该模块包含在多个模型中。在我们的(规范)测试中,此结果输出到警告消息中,例如:/home/ayrton/project/lib/life_cycle.rb:5:warning:alreadyinitializedconstantRESET解决这个问题的一种方法是,像这样声明常量:moduleLifeCycleunless(const_defined?(:RESET))RESET='reset'end#...end我还阅读了AvdiGrimm撰写的博客文章,其中提供了替代方案solution,我想知道您

ruby Rspec : Testing instance variables without adding an accessor to source

我正在尝试测试以下方法:defunprocess_move(board,move)ifmove[0].instance_of?(Array)multi_move=@multi_move.pop(2).reversemulti_move.eachdo|single_move|unapply_move(board,single_move)endelseboard=unapply_move(board,move)endboardend我想为@multi_move设置状态,但我不想添加仅用于测试的访问器。有没有办法在没有访问器的情况下这样做?谢谢。 最佳答案

Ruby 元编程 : dynamic instance variable names

假设我有以下哈希:{:foo=>'bar',:baz=>'qux'}我如何动态设置键和值以成为对象中的实例变量...classExampledefinitialize(hash)...magichappenshere...endend...这样我就可以在模型中得到以下内容...@foo='bar'@baz='qux'? 最佳答案 您要找的方法是instance_variable_set.所以:hash.each{|name,value|instance_variable_set(name,value)}或者,更简单地说,hash.e

ruby :kind_of?与 instance_of?与 is_a?

有什么区别?我什么时候应该使用哪个?为什么有这么多? 最佳答案 kind_of?和is_a?是同义词。instance_of?与其他两个的不同之处在于它仅在对象是该类的实例而不是子类的实例时才返回true。例子:"hello".is_a?对象和"hello".kind_of?Object返回true因为"hello"是一个String而String是的子类>对象。但是“hello”.instance_of?对象返回false。 关于ruby:kind_of?与instance_of?与i

javascript/jquery 错误 "invalid object initializer"

我正在调用这样的函数:myfunc($tab,{'top-left','bottom-left'},defaults.tabRounded);函数定义为:functionmyfunc(obj,properties,value){但我收到错误“无效的对象初始值设定项”。这是因为json参数吗?还是别的? 最佳答案 替换myfunc($tab,{'top-left','bottom-left'},defaults.tabRounded);与myfunc($tab,['top-left','bottom-left'],defaults.t

javascript - this.initialize(arguments) 与 this.initialize.apply(this, arguments) : what's the difference?

如果您查看Backbone.js的源代码,您会看到此模式的多种用途:this.initialize.apply(this,arguments);例如,这里:varRouter=Backbone.Router=function(options){options||(options={});if(options.routes)this.routes=options.routes;this._bindRoutes();this.initialize.apply(this,arguments);};为什么不直接写this.initialize(arguments)呢?

javascript - 传单 : Map container is already initialized does not get solved by proposed answers

我正在尝试使用传单加载map。当我刷新map时,出现上述错误。我研究了这个问题的其他建议答案。但是,他们中没有一个对我有用。我正在尝试在由onclick事件运行的函数中加载map。这是代码:functionload_map_and_analyze_data(){varmymap=L.map('mapid',{center:newL.LatLng(the_center_splitted[0],the_center_splitted[1]),maxZoom:17,minZoom:11,zoom:14});//creatingthemap//therestofanalyzeandcodego