在ruby中使用`除非’的`next`时执行顺序混淆

next语句用于跳过循环的一部分并继续循环的下一次迭代。 它可以与for和while语句结合使用。 我看到人们使用next,如果在评估某些条件后存在复杂的代码,即 next if @state!=:some_state # some long complicated code 现在我在IRB玩了next ,如下所示: n = 1 loop do n = n + 1 next unless n == 10 print “Good” break end # Good=> nil 以上是理解的。 很清楚。 n = 1 #=> 1 loop do print “#{n}” n = n + 1 next puts “hi” unless […]

强参数 – 无法访问深层嵌套属性

一个StackOverflow问题询问了我需要什么,但自我答案并没有帮助我知道下一步该做什么。 在那个问题中呈现的场景( 在铁轨中将深层嵌套的强参数列入白名单 )几乎就是我所发生的事情,但我会发布一个我的缩写(仍然很长)并希望有人 – 甚至可能是戴夫post – 可以帮助(我没有足够的声誉评论和问他)。 关于嵌套的强参数的链接很少,我还没有读过,我在许多控制器和API端点上处理了一些,但这是应用程序中最复杂的。 (我包含了这么长的例子,所以你可以看到完整的复杂性。) 这是在sales_controller ,我们无法获得的属性之一是timezone_name ,它位于run_spans_attributes ,位于sale下的options_attributes 。 我已经尝试了几乎所有与嵌入式属性匹配的不同语法方法,这些方法在StackOverflow上有强参数问题,但没有一种方法有效。 我需要更多课程吗? 有魔术括号吗? 我需要新的建议。 请。 应该注意的是,这是使用strong_parameters gem和Rails 3.2.21,但我希望为Rails 4准备应用程序,所以我希望避免短期解决方案。 对不起,这么久了: Parameters: “sale”=>{ “cloned_from”=>””, “type”=>”Localsale”, “primary_contact_attributes”=>{ “primary”=>”true”, “first_name”=>”Fred”, “id”=>”1712” }, “contract_signed_on”=>”March 20, 2015”, “billing_addresses_attributes”=>{ “0”=>{ “billing”=>”1”, “city”=>”San Diego”, “id”=>”29076” } }, “other_contacts_attributes”=>{ “0”=>{ “first_name”=>”Fred”, “_destroy”=>”false”, “id”=>”170914” }, “1”=>{ “first_name”=>”Fred”, “last_name”=>”Smith”, “_destroy”=>”false”, […]

元编程访问局部变量

class Foo def initialize bar = 10 end fiz = 5 end 是否有可能获得这些本地值(课外)?

Rails 4 – 未定义的方法’authenticate’为nil:NilClass

我正在开发一个使用Rails 4和Devise构建社交网络的课程。 我试图让课程通过,这是我得到的错误: 1) Error: UserFriendshipsControllerTest#test_: #new when not logged in should get redirected to the login page. : NoMethodError: undefined method `authenticate!’ for nil:NilClass test/controllers/user_friendships_controller_test.rb:8:in `block (3 levels) in ‘ 这是我的users_friendships_controller_test.rb: require ‘test_helper’ class UserFriendshipsControllerTest < ActionController::TestCase context "#new" do context "when not logged in" do should "get redirected to the login page" do […]

Ruby .split()正则表达式

我正在尝试将字符串”[test| blah] [foo |bar][test|abc]”拆分为以下数组: [ [“test”,”blah”] [“foo”,”bar”] [“test”,”abc”] ] 但我无法正确表达我的正则表达式。 ruby: @test = ‘[test| blah] [foo |bar][test|abc]’.split(%r{\s*\]\s*\[\s*}) @test.each_with_index do |test, i| @test[i] = test.split(%r{\s*\|\s*}) end 我不在那里,这回来了: [ [ “[test” , “blah” ] [ “foo” , “bar” ] [ “test” , “abc]” ] ] 实现这一目标的正确正则表达式是什么? 如果我还可以考虑新行,那就太好了,比如说: “[test| blah] \n [foo |bar]\n[test|abc]”

编写多行懒惰计算方法的ruby方法是什么?

我认为我可能不会以最ruby的方式编写懒惰的实例化方法/属性。 以此方法为例: def tax @tax ||= Proc.new do if flat_tax > commission_plan.tax_max return commission_plan.tax_max end if commission_plan.tax_max if flat_tax < commission_plan.tax_min return commission_plan.tax_min end if commission_plan.tax_min flat_tax end.call end 是否有一种更像ruby的方法来重构这种方法?

DataMapper因关系而无法删除记录

我有许多使用Torrent和Tag的DataMapper / MySQL设置,如下所示: class Torrent include DataMapper::Resource property :id, Serial property :name, String property :magnet, Text property :created_at, DateTime has n, :tags, :through => Resource end class Tag include DataMapper::Resource property :id, Serial property :name, String property :hits, Integer has n, :torrents, :through => Resource end 但是,当试图通过Torrent.first.destroy或类似的东西来破坏一个torrent时,DataMapper会返回false 。 我尝试了直接的SQL查询,例如delete from torrents where name like ‘%ubuntu%’ […]

从ruby脚本执行shell命令

我正在尝试做这样的事情 listen_for /turn on the living room lights/i do system(‘/usr/bin/tdtool -n 1’) say “Your lights is now turned on” request_completed end 但是系统内部的命令没有运行,而是收到以下消息: 打开设备1, – 设备不支持您尝试使用的方法 如果我从shell运行完全相同的方法,它的效果很好,任何想法?

无法加载require_dependency

我正在尝试在ActiveSupport中使用ActiveSupport库中的require_dependency : require ‘active_support’ #=> true require_dependency ‘test’ #=> NoMethodError: undefined method #=> `require_dependency’ for main:Object 可能是什么问题呢?

在能够使用Capistrano进行部署之前,是否需要在我的服务器上手动安装Bundler?

标题基本概括了所有内容。 我刚刚使用Ruby 1.9.2设置了我的Web服务器。 这是一个干净的安装 – 尚未安装任何gem。 另外,我刚刚为我的Rails应用程序配置了Capistrano。 当我运行cap deploy ,它失败并显示以下错误消息: *** [err :: bogusip.com] bash: bundle: command not found 鉴于我所做的就是将Ruby 1.9.2安装到我的服务器上,此时Bundler不存在。 Capistrano是应该为我安装Bundler gem还是我自己应该这样做?