Skip to content

Instantly share code, notes, and snippets.

@mathiasjakobsen
Last active February 3, 2016 21:05
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 mathiasjakobsen/25c3845016abf750c409 to your computer and use it in GitHub Desktop.
Save mathiasjakobsen/25c3845016abf750c409 to your computer and use it in GitHub Desktop.
A collection of some useful Ruby methods and (partly) dirty tricks.
# Initilize an array containing plucked property of each hash in array
array_of_hashes = [] << array_of_hashes.map(&:hash_property)
# Generate a random alphanumeric string, with a fixed length
[*('a'..'z'), *('0'..'9')].sample(15).join
# Find average of list of intergers, read from STDIN
avg, n = 0.0, 1.0
arr = STDIN.read.split("\n").map(&:to_i)
arr.each { |i| avg = (n - 1) / n * avg + i / n; n += 1.0 }
avg # => Average
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment