Advertisement
Guest User

Rubypaste

a guest
Nov 13th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.17 KB | None | 0 0
  1. [dan@crystal ~]$ irb
  2. 2.1.3 :001 > x = Array.new(20){rand(100)}
  3.  => [61, 93, 56, 29, 60, 36, 2, 28, 20, 25, 98, 2, 55, 68, 69, 57, 53, 92, 77, 49]
  4. 2.1.3 :002 > y = x.each_with_index {|k, v| p "#{k} => #{v}" }
  5. "61 => 0"
  6. "93 => 1"
  7. "56 => 2"
  8. "29 => 3"
  9. "60 => 4"
  10. "36 => 5"
  11. "2 => 6"
  12. "28 => 7"
  13. "20 => 8"
  14. "25 => 9"
  15. "98 => 10"
  16. "2 => 11"
  17. "55 => 12"
  18. "68 => 13"
  19. "69 => 14"
  20. "57 => 15"
  21. "53 => 16"
  22. "92 => 17"
  23. "77 => 18"
  24. "49 => 19"
  25.  => [61, 93, 56, 29, 60, 36, 2, 28, 20, 25, 98, 2, 55, 68, 69, 57, 53, 92, 77, 49]
  26. 2.1.3 :003 > y
  27.  => [61, 93, 56, 29, 60, 36, 2, 28, 20, 25, 98, 2, 55, 68, 69, 57, 53, 92, 77, 49]
  28.  
  29.  
  30. 2.1.3 :044 > x.each do |e|
  31. 2.1.3 :045 >     y.push(e)
  32. 2.1.3 :046?>   y.push(i)
  33. 2.1.3 :047?>   i = i + 1
  34. 2.1.3 :048?>   end
  35.  => [89, 37, 3, 25, 82, 69, 70, 50, 58, 47, 43, 95, 46, 72, 20, 60, 77, 48, 47, 12]
  36. 2.1.3 :049 > y
  37.  => [89, 1, 37, 2, 3, 3, 25, 4, 82, 5, 69, 6, 70, 7, 50, 8, 58, 9, 47, 10, 43, 11, 95, 12, 46, 13, 72, 14, 20, 15, 60, 16, 77, 17, 48, 18, 47, 19, 12, 20]
  38. 2.1.3 :050 > z=Hash[*y.flatten]
  39.  => {89=>1, 37=>2, 3=>3, 25=>4, 82=>5, 69=>6, 70=>7, 50=>8, 58=>9, 47=>19, 43=>11, 95=>12, 46=>13, 72=>14, 20=>15, 60=>16, 77=>17, 48=>18, 12=>20}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement