Last active
February 3, 2016 21:05
-
-
Save mathiasjakobsen/25c3845016abf750c409 to your computer and use it in GitHub Desktop.
A collection of some useful Ruby methods and (partly) dirty tricks.
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
# 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