草庐IT

index_name

全部标签

ruby - 如何在 ruby​​ 中编写负循环,如 for(i=index; i >= 0; i --)

如何在Ruby中编写一个倒计时循环,类似于以下C风格的for循环?for(i=25;i>=0;i--){printi;} 最佳答案 在Ruby中有很多方法可以执行递减循环:第一种方式:foriin(10).downto(0)putsiend第二种方式:(10).downto(0)do|i|putsiend第三种方式:i=10;untili 关于ruby-如何在ruby​​中编写负循环,如for(i=index;i>=0;i--),我们在StackOverflow上找到一个类似的问题:

Ruby 数组 each_slice_with_index?

如果我有arr=[1,2,3,4]我知道我可以执行以下操作...>arr.each_slice(2){|a,b|puts"#{a},#{b}"}1,23,4...和...>arr.each_with_index{|x,i|puts"#{i}-#{x}"}0-11-22-33-4...但是是否有内置的方法来做到这一点?>arr.each_slice_with_index(2){|i,a,b|puts"#{i}-#{a},#{b}"}0-1,22-3,4我知道我可以构建自己的并将其粘贴到数组方法中。只是想看看是否有内置函数可以执行此操作。 最佳答案

ruby-on-rails - Gem.source_index 已弃用,请使用 Specification。我应该重新安装 Gem 还是 Rails?

我正在Ubuntu11上学习RoR。当我尝试生成应用程序时收到以下消息。我是不是安装错了什么?$railsgeneratecontrollerPageshomecontactNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/usr/lib/ruby/gems/1.8/gems/bundler-1.0.12/lib/bundler/shared_helpers.rb:3.NOTE:Gem.source_indexi

ruby - 在 Ruby 中,有没有办法使用类似 hash.each_with_index do |[k,v], i| 的方法?

否则就需要h={:a=>1,:b=>2.2}h.each_with_indexdo|pair,i|k=pair[0];v=pair[1]pk,v,iend并以这种方式设置k和v似乎有点笨拙。它可以更简单还是类似h.each_with_indexdo|[k,v],i|? 最佳答案 事实上,是的!使用括号:h={:a=>1,:b=>2.2}h.each_with_indexdo|(k,v),i|pk,v,iend 关于ruby-在Ruby中,有没有办法使用类似hash.each_with_i

ruby-on-rails - ruby rails : How do you explicitly define plural names and singular names in Rails?

例如,我使用“Bonus”作为我的模型,所以我希望“bonuses”是复数形式而“bonus”是单数形式。但是,在Ruby中,这会导致:"bonus".pluralize#bonus"bonuses".singularize#bonuse因此,例如,当我执行“has_many:bonuses”时,它不会使用Bonus.rb模型(因为Ruby需要Bonuse.rb模型)。有没有一种方法可以在RubyonRails中以某种方式更正这一点,使“bonuses”充当模型bonus.rb的复数形式? 最佳答案 在config/initiali

ruby-on-rails - Rails : named_scope, lambda 和 block

我认为下面两个是等价的:named_scope:admin,lambda{|company_id|{:conditions=>['company_id=?',company_id]}}named_scope:admin,lambdado|company_id|{:conditions=>['company_id=?',company_id]}end但Ruby正在提示:ArgumentError:triedtocreateProcobjectwithoutablock有什么想法吗? 最佳答案 这是一个解析器问题。试试这个named_s

ruby - Ruby 中 each.with_index 和 each_with_index 的区别?

我真的很困惑each.with_index和each_with_index之间的区别。它们有不同的类型,但在实践中似乎是相同的。 最佳答案 with_index方法采用可选参数来偏移起始索引。each_with_index做同样的事情,但没有可选的起始索引。例如:[:foo,:bar,:baz].each.with_index(2)do|value,index|puts"#{index}:#{value}"end[:foo,:bar,:baz].each_with_indexdo|value,index|puts"#{index}:

ruby each_with_index 偏移量

我可以在each_with_index循环迭代器中定义索引的偏移量吗?我的直接尝试失败了:some_array.each_with_index{|item,index=1|some_func(item,index)}编辑:澄清:我不想要数组偏移我希望each_with_index中的索引不是从0开始,而是例如1. 最佳答案 实际上,Enumerator#with_index接收偏移量作为可选参数:[:foo,:bar,:baz].to_enum.with_index(1).eachdo|elem,i|puts"#{i}:#{elem

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 - map(& :name) mean in Ruby? 是什么意思

我在aRailsCast中找到了这段代码:deftag_names@tag_names||tags.map(&:name).join('')endmap(&:name)中的(&:name)是什么意思? 最佳答案 它是tags.map(&:name.to_proc).join('')的简写如果foo是一个带有to_proc方法的对象,那么你可以将它作为&foo传递给一个方法,它将调用>foo.to_proc并将其用作方法block。Symbol#to_proc方法最初由ActiveSupport添加,但已集成到Ruby1.8.7中。这