Problem: Trying to merge two Ranges into a Hash



I'm trying to take two ranges (namely, (1..26) and ('A'..'Z')) and
merge them into a new hash such that A=>1, B=>2, etc. I know I could
easily do this with a loop but I want to use an iterator and I'm not
quite sure how to approach it. I know this isn't right but it's the
best I ccould come up with:

letters = ('A'..'Z')
numbers = (1..26)
letter_to_number = {}
numbers.each, letters.each {|num, let| letter_to_number[let => num]}

If I use that, I get "1solitaire.rb:32: parse error, unexpected '\n',
expecting tCOLON2 or '[' or '.'"
(line 32 is the last line in the code above)

I'm rather new to Ruby but so far I'm very impressed with it as well as
the community - thanks for any pointers :)

.



Relevant Pages

  • Re: STL Vector: Unexpected behavior
    ... iterator of that container. ... you could move all pointers you want to keep to a new ... you also omit checking if the pointer is zero in your loop. ...
    (microsoft.public.vc.stl)
  • Re: iterator invalidation trouble
    ... Iterator invalidation. ... Thus the iterator it gets invalid. ... > simply introduce a second iterator inside the for loop. ... I made the container have pointers to the objects and once the user was ...
    (comp.lang.cpp)
  • Re: Factoring iteration
    ... Now it's cool that the double-nested loop turns into just the one line ... grid iterator class, where the actual per-item action is a method, ... All masks with only one bit set represent a solution for that field. ... actually requires backtracking, unless you want to find all possible ...
    (comp.lang.forth)
  • Re: std::vector : begin, end and insert - Using Objects instead of ints
    ... When there's a choice, prefer preincrement to postincrement, because pre ... The thing is, efficiency can be measured objectively, while "readability" ... split the operation into two statements, e.g. when erasing a list iterator. ... certain kind of loop that omits stmt3 in a for-loop header when you decide ...
    (microsoft.public.vc.mfc)
  • Re: Generics and for each
    ... so the compiler has no way of matching the 'String' type of the loop ... I used 'String' to ... stands it fails because it doesn't know that the Iterator is a ...
    (comp.lang.java.programmer)