Created
April 7, 2011 00:27
-
-
Save peterhellberg/906817 to your computer and use it in GitHub Desktop.
Fun with the Ruby 1.9 Standard Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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