From: William James Date: 2006-03-07T08:38:40+09:00 Subject: Re: What is the best way to iterate through two containers of the same length? dblack@wobblini.net wrote: > Hi -- > > On Tue, 7 Mar 2006, William James wrote: > > > Mark Watson wrote: > >> If I have two containers c1 and c2 of the same length, what is the > >> proper "Ruby way" to do this: > >> > >> c1.length.times {|i| > >> # access c1[i], c2[i] > >> } > >> > >> I like that Ruby container classes provide their own iterators, but > >> what I would like to have is something like: > >> > >> (c1,c2).each {|x1,x2| .... } > >> > >> I thought of writing my own iterator class so that I could do something > >> like: > >> > >> Iterator.new(c1,c2).each {|x1,x2| .... } > >> > >> but that looks clumsy and inefficient. > >> > >> I am transitioning to using mostly Ruby (moving away from Java, Lisp, > >> and Smalltalk) and I would like to use the proper Ruby idioms. > > > > foo = %w(x y z) ; bar = [2,4,6] > > [foo, bar].transpose.each{|a,b| print a, b, $/ } > > ---> > > x2 > > y4 > > z6 > > I don't think $/ is very idiomatic. See the ToDo file in the source; > it includes: > > * discourage use of symbol variables (e.g. $/, etc.) in manual > > :-) > > > David idiomatic, adj. Peculiar to a particular group or individual. "\n" is found in C and in awk, but $/ isn't; so it is more nearly peculiar to Ruby. Some may lack the capacity to remember what it represents. However, I have no doubt that this is an unfashionable opinion and that your view is the dominant one. -- idiot, n. A member of a large and powerful tribe whose influence in human affairs has always been dominant and controlling. ... He sets the fashions of opinion and taste, dictates the limitations of speech, and circumscribes conduct with a dead-line.