草庐IT

pattern-matching-with-if-case

全部标签

ruby-on-rails - rails 4 : How to use includes() with where() to retrieve associated objects

我不知道如何使用.where()检索关联模型数据的方法。在此示例中,项目belongs_to用户...classProjectparams[:id]}).firstendend在App/views/projects/invite.html.erg返回:---!ruby/object:Projectattributes:id:22name:SomeProjectNamebelongs_to:1instructions:Blablablaactive:truemax_duration:2max_videos:created_at:2013-08-2615:56:50.000000000Zu

ruby .拒绝!与.delete_if

作为Ruby的新手,我对.reject之间的区别有疑问!和.delete_if处理哈希和数组时的方法。如果只是想摆脱某些对象,这些方法在功能上有什么区别吗?以及为什么要使用一个而不是另一个?谢谢!编辑我已经阅读了文档......我想我应该在我原来的问题中更清楚。我想知道更多关于效率差异的信息。他们删除项目的方式是否不同?(同样,忽略返回值。我知道这是不同的。谢谢!) 最佳答案 reject-创建一个没有元素匹配的新数组并返回新数组delete_if-从当前数组中删除匹配的元素并返回数组reject!-从当前数组中删除匹配的元素。如果

ruby-on-rails - 如何测试依赖: :destroy with RSpec?

我想测试我的User模型关联has_many:projects,dependent::destroy现在已经走了这么远:it"destroysdependentprojects"douser=FactoryGirl.build(:user)project=FactoryGirl.build(:project)user.projects但这给出了一个错误:Failure/Error:expect(Project.count).tochange(-1)ArgumentError:`change`requireseitheranobjectandmessage(`change(obj,:ms

Ruby if .. elsIf .. else 在一行中?

使用ruby​​三元运算符,我们可以为简单的ifelse构造编写以下逻辑:a=true?'a':'b'#=>"a"但是如果我想把它写成iffoo'a'elsifbar'b'else'c'怎么办?我可以这样写,但是有点难理解:foo=truea=foo?'a':(bar?'b':'c')#=>"a"foo=falsebar=truea=foo?'a':(bar?'b':'c')#=>"b"是否有更好的选择来处理这种情况,或者如果我们希望将if..elsif..else逻辑压缩到一行中,这是我们最好的选择吗? 最佳答案 a=(foo&&

ruby-on-rails - ruby 正则表达式错误 : incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

我遇到了两个错误,都与编码有关并且都相关。我在启动WEBrick时遇到的第一个错误(技术上是警告):/Users/USERNAME/example/config/initializers/bb-ruby.rb:54:warning:invalidUnicodeProperty\P:/\:\-?\P/它所指的行是:/\:\-?\P/,这只是一些正则表达式,最终是这个block的一部分:@@tags['Razzing']=[/\:\-?\P/,'','Razzing',':P',:razzing]然后,我在解析一些字符串时也得到了以下错误(大概是由于同一行)...Encoding::Com

ruby-on-rails - ruby /rails : How to determine if module is included?

在这里扩展我的问题(ruby/rails:extendingorincludingothermodules),使用我现有的解决方案,确定我的模块是否包含在内的最佳方法是什么?我现在所做的是在每个模块上定义实例方法,这样当它们被包含时,一个方法就可用,然后我只是向父模块添加一个捕获器(method_missing())所以如果它们不包括在内,我可以catch。我的解决方案代码如下:moduleFeaturesFEATURES=[Running,Walking]#includeFeatures::RunningFEATURES.eachdo|feature|includefeatureen

ruby - each_with_index_do 索引从 1 开始

我在Rails应用程序的View上使用ruby​​迭代器,如下所示:...我想加上1..而不仅仅是说:@document.data会得到让上面的索引从1开始的技巧。但是,遗憾的是,上面的代码索引仍然是0到data.length(实际上是-1)。那么我做错了什么,我需要索引等于1-data.length...不知道如何设置迭代器来执行此操作。 最佳答案 除非你使用像1.8这样的旧Ruby(我认为这是在1.9中添加的,但我不确定),你可以使用each.with_index(1)来获得1-基于枚举器:在你的情况下它会是这样的:...

关于Document mapping type name can‘t start with ‘_‘, found: [_update]

在修改elasticsearch时,用_update进行局部修改,修改失败,报错{    "error": {        "root_cause": [            {                "type": "invalid_type_name_exception",                "reason": "Document mapping type name can't start with '_', found: [_update]"            }        ],        "type": "invalid_type_name_exce

ruby - next 与 if 在 .each 循环中?

我正在用Ruby进行文本处理。基本上,我必须实现一个简单的状态机(带有一个字符后视)。我现在的代码是这样的:text.each{|c|............if@state!=:some_statenextend#processingstuffforifin:some_statemode...............}这样合适吗?或者它应该像这样实现:text.each{|c|............if@state==:some_state#processingstuffforifin:some_statemode...............end}有正确的方法还是只是偏好?哪个

ruby - 为什么即使未执行该代码路径,Ruby 似乎也会从 case 语句内部提升变量声明?

这个问题在这里已经有了答案:WhycanIrefertoavariableoutsideofanif/unless/casestatementthatneverran?(3个答案)关闭5年前。我们定义一个函数foo:deffoo(s)caseswhen'foo'x=3putsx.inspectwhen'bar'y=4putsy.inspectendputsx.inspectputsy.inspectend然后我们这样调用它:1.9.3p194:017>foo('foo')infooscope3inouterscope3nil=>nil1.9.3p194:018>foo('bar')in