不断有人告诉我,我正在写" C形式的迭代器"在Ruby中,而且我应该做这种其他的方式,没有我的事++ [英] People keep telling me I'm writing "C style iterators" in ruby, and that I'm supposed to do it this other way, without the i++ thing

查看:106
本文介绍了不断有人告诉我,我正在写" C形式的迭代器"在Ruby中,而且我应该做这种其他的方式,没有我的事++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的细节并不重要,这只是一个例子,所有的事情是I = 0,I + = 1的动作

 高清sortAndIndex
  #sorting键
  @disco =哈希[@ disco.sort]  #basic设置为输入类型
  @years = @ disco.keys
  @albums = @ disco.values
  综上所述= @ years.count  #setup的相册输入
  @allalbums = []
  I = 0
  sum.times做
  thatyear = @years [I] +,+ @albums [I]
  @allalbums<<那年
  I + = 1
 结束
结束

现在这样做工作,如果有任何其他的i ++型(C风格),迭代器,Ruby或大多数其他语言。不过,我一直听说的红宝石有这更好的方式来做到这一点,在那里基本上我会比路过这里,'A'变量以外的第二个参数。

我会做什么修改,要做到这一点,并有多个捷径,这Ruby有涉及这些各种各样的我++元[I]迭代器的问题?


解决方案

是的,你应该做的事情红宝石路,而忘记了索引。为什么?因为它是地道的和看起来更好。你传递时集合的每个元素将被执行code的块。元素的存储方式,在什么样的顺序,如果他们能够被索引到,都是无关紧要的。

 #不错,不是吗?
some_array.each做| ELEM |
  some_action(ELEM)
结束

如果你需要一个索引计数器(你在你的例子不)使用 each_with_index 代替。

另外请注意,块的概念是重要的红宝石迭代集合之外。你有什么是... ...奇怪,你应该听他们的,但很多方法取块作为参数(或可选参数)不返回的集合可以被索引。例如, File.open 需要一个块,如果提供,关闭文件为你当块返回

另外,还有一点要注意;命名约定是典型的Ruby。 Ruby开发者使用小写函数和变量名与 underscores_between_words 。最好坚持使用这里的社区。

The details here aren't important, it's just an example, all that matters is the i=0, i+=1 action

def sortAndIndex
  #sorting keys
  @disco = Hash[@disco.sort]

  #basic setup for both input types
  @years = @disco.keys
  @albums = @disco.values
  sum = @years.count

  #setup for "albums" input
  @allalbums = []
  i = 0
  sum.times do
  thatyear = @years[i] + ", " + @albums[i]
  @allalbums << thatyear
  i += 1
 end
end

Now this does work, as should any other "i++" type ("C-style") iterator in ruby or most other languages. However, I keep hearing that ruby has this better way to do this, where basically I'd be passing a second parameter variable other than, here, 'a'.

What modifications would I make to do this and are there multiple "shortcuts" which ruby has involving these sorts of i++ ary[i] iterator problems?

解决方案

Yes, you should do things the "Ruby way" and forget about indexing. Why? Because it is idiomatic and looks nicer. You pass in a block of code to be executed upon each element of a collection. How the elements are stored, in what order, if they can be indexed into, is all irrelevant.

# nice, eh?
some_array.each do |elem|
  some_action(elem)
end

If you need an index counter (you don't in your example) use each_with_index instead.

Also note that the concept of blocks is important in Ruby outside of iterating a collection. What you have there is... odd... and you should listen to them, but many methods take blocks as arguments (or optional arguments) which do not return collections which can be indexed. For example, File.open takes a block and, if supplied, closes the file for you when the block returns

Also, another thing to note; your naming conventions are atypical for Ruby. Rubyists use lowercase function and variable names with underscores_between_words. Best to stick with the community here.

这篇关于不断有人告诉我,我正在写&QUOT; C形式的迭代器&QUOT;在Ruby中,而且我应该做这种其他的方式,没有我的事++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆