假设我有一组数字,例如ary=[1,3,6,7,10,9,11,13,7,24]我想在较小数字跟随较大数字的第一个点之间拆分数组。我的输出应该是:[[1,3,6,7,10],[9,11,13,7,24]]我已经尝试了slice_when,结果非常接近:ary.slice_when{|i,j|i>j}.to_a#=>[[1,3,6,7,10],[9,11,13],[7,24]]但它也在13和7之间拆分,所以我必须加入剩余的数组:first,*rest=ary.slice_when{|i,j|i>j}.to_a[first,rest.flatten(1)]#=>[[1,3,6,7,10],
我正在尝试创建一个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