Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.