1. Integer#digits
123.to_s.chars.map(&:to_i).reverse
=> [3, 2, 1]
123.digits
=> [3,2,1]
2. Array#values_atarr = [1,2,3,4,5]
a, b, c = arr[0], arr[1], arr[4]
a, b, c = arr.values_at(0, 1, 4)
hash = {bacon: 300, chocolate: 200}
hash.values_at(:bacon, :chocolate)
=> [300, 200]
3. Hash#transform_values
h = {bacon: 200, coconut: 300}h.each { |k,v| h[k] = v*2 }
h.transform_values! { |v| v * 2 }
{:bacon=>400, :coconut=>600}
4. Array#count
letters = %w(a a a b c d a)letters.count("a")
=> 4
numbers = [1,2,3,4,5,6]
numbers.count(&:even?)
=> 3
Comments
Post a Comment