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 -


Details are not important here, this is just an example, in all cases it is i = 0, i + = 1 verb def sortAndIndex #sorting keys @disco = Hash [@ disco.sort] #yasic setup for both types of inputs @years = @ disco.keys @albums = @ disco.values Sum = @years "album" input @allalbums = [] for .count #setup I = 0 sum.times do thatearear = @years [i] + ", + @albums [i] @allalbums & lt; & Lt; Now it works, as in any other "i ++" type ("C-style") Iterator in Ruby or other languages ​​I / I + = 1 end end

However, I'm hearing that Ruby has a better way of doing this, where basically I pass the 'A', leaving the second parameter variable.

What amendments will I make to do this and there are several "shortcuts" that have included these types in Ruby I ++ ary [i] iterator problems?

Yes, you should do "ruby path" and forget about indexing. Why? Because it is idiotic and looks good You pass in each block of code executed on each element of the collection. How elements are stored, in what order, if they can be indexed, then all this is irrelevant.

  # Well, right? Some_array.each do | Elem | Some_Actions (AMM) End   

If you need an index counter (not in your example) then use every_in_index instead.

Also note that Ruby's concept is important outside of a collection. What do you have ... strange ... and you should listen to them, but block many methods in the form of logic (or alternative argument) that do not return the archive which can be indexed. For example, File.open takes a block, and if the supply is made, the block closes the file for you for returns

In addition, another Note the matter; There are specifications of your naming conventions for Ruby: Rubyists use the Locercus Function and Variable name, in which underscores_between_words is the best way to live with the community here.

Comments