Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created April 7, 2011 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/906817 to your computer and use it in GitHub Desktop.
Save peterhellberg/906817 to your computer and use it in GitHub Desktop.
Fun with the Ruby 1.9 Standard Library
require 'set'
# Sorted list of random numbers
pp 10.times.each_with_object(SortedSet.new) { |n, s| s << Random.new.rand(1..100) }
# Associative arrays
aoa = ('a'..'l').each_slice(2).to_a
Hash[aoa] #=> {"a"=>"b", "c"=>"d", "e"=>"f", "g"=>"h", "i"=>"j", "k"=>"l"}
aoa.assoc('a') #=> ["a", "b"]
aoa.rassoc('d') #=> ["c", "d"]
aoa.unshift(["a", "x", "y"]) #=> [["a", "x", "y"], ["a", "b"], ["c", "d"], ["e", "f"], ["g", "h"], ["i", "j"], ["k", "l"]]
aoa.assoc('a') #=> ["a", "x", "y"]
aoa.shift #=> ["a", "x", "y"]
aoa.assoc('a') #=> ["a", "b"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment