What are your favorite code snippets with ruby collections? Preferably they should be discovery for you, be expressive, readable and introduce some fun in your coding practice.
Pattern-matching in arrays (for local variables and parameters):
(a, b), c = [[:a, :b], :c]
[a,b,c]
=> [:a, :b, :c]
(a,), = [[:a]]
a
=> :a
Assigning from non-arrays to multiple variables:
abc, a, b =* "abc".match(/(a)(b)./)
=> ["abc", "a", "b"]
nil1, =* "abc".match(/xyz/)
=> []
Initialize array elements with the same expression:
5.times.map { 1 }
=> [1,1,1,1]
Array.new(5) { 1 }
=> [1,1,1,1,1]
Initialize array with the same value:
[2]*5
=>[2,2,2,2,2]
Array.new 5, 2
=>[2,2,2,2,2]
Sum elements of an array:
[1,2,3].reduce(0, &:+)
=> 6
Find all indices that match condition:
a.each_with_index.find_all { |e, i| some_predicate(e) }.map(&:last)
Alternate CSS classes:
(1..4).zip(%w[cls1 cls2].cycle)
=> [[1, "cls1"], [2, "cls2"], [3, "cls1"], [4, "cls2"]]
Unzipping:
keys, values = {a: 1, b: 2}.to_a.transpose
keys
=> [:a, :b]
Exploring boolean member methods of a string:
"".methods.sort.grep(/\?/)
Exploring string-specific methods:
"".methods.sort - [].methods
Lazy Fibonacci series with memoization, taken from Neeraj Singh:
fibs = { 0 => 0, 1 => 1 }.tap do |fibs|
fibs.default_proc = ->(fibs, n) { fibs[n] = fibs[n-1] + fibs[n-2] }
end
fibs.take(10).map(&:last).each(&method(:puts))
An implementation of Counting Sort:
module Enumerable
def counting_sort(k)
reduce(Array.new(k+1, 0)) {|counting, n| counting.tap { counting[n] += 1 }}.
map.with_index {|count, n| [n] * count }.flatten
end
end
An implementation of sum
aka prefix sum:
module Enumerable
def scan(initial=nil, sym=nil, &block)
args = if initial then [initial] else [] end
unless block_given?
args, sym, initial = [], initial, first unless sym
block = ->(acc, el) { acc.send(sym, el) }
end
[initial || first].tap {|res|
reduce(*args) {|acc, el|
block.(acc, el).tap {|e|
res << e
}
}
}
end
end
Here, I experimented with having Hash#each
yield KeyValuePair
s instead of two-element Array
s. It's quite surprising, how much code still works, after doing such a brutal monkey-patch. Yay, duck typing!
class Hash
KeyValuePair = Struct.new(:key, :value) do
def to_ary
return key, value
end
end
old_each = instance_method(:each)
define_method(:each) do |&blk|
old_each.bind(self).() do |k, v|
blk.(KeyValuePair.new(k, v))
end
end
end
Something I have been playing around with is making Enumerable#===
perform recursive structural pattern matching. I have no idea if this is in any way useful. I don't even know if it actually works.
module Enumerable
def ===(other)
all? {|el|
next true if el.nil?
begin
other.any? {|other_el| el === other_el }
rescue NoMethodError => e
raise unless e.message =~ /any\?/
el === other
end
}
end
end
Another thing I toyed around with recently was re-implementing all methods in Enumerable
, but using reduce
instead of each
as the basis. In this case, I know it doesn't actually work properly.
module Enumerable
def all?
return reduce(true) {|res, el| break false unless res; res && el } unless block_given?
reduce(true) {|res, el| break false unless res; res && yield(el) }
end
def any?
return reduce(false) {|res, el| break true if res || el } unless block_given?
reduce(false) {|res, el| break true if res || yield(el) }
end
def collect
reduce([]) {|res, el| res << yield(el) }
end
alias_method :map, :collect
def count(item=undefined = Object.new)
return reduce(0) {|res, el| res + 1 if el == item } unless undefined.equal?(item)
unless block_given?
return size if respond_to? :size
return reduce(0) {|res, el| res + 1 }
end
reduce(0) {|res, el| res + 1 if yield el }
end
def detect(ifnone=nil)
reduce(ifnone) {|res, el| if yield el then el end unless res }
end
alias_method :find, :detect
def drop(n=1)
reduce([]) {|res, el| res.tap { res << el unless n -= 1 >= 0 }}
end
def drop_while
reduce([]) {|res, el| res.tap { res << el unless yield el }}
end
def each
tap { reduce(nil) {|_, el| yield el }}
end
def each_with_index
tap { reduce(-1) {|i, el| (i+1).tap {|i| yield el, i }}}
end
def find_all
reduce([]) {|res, el| res.tap {|res| res << el if yield el }}
end
alias_method :select, :find_all
def find_index(item=undefined = Object.new)
return reduce(-1) {|res, el| break res + 1 if el == item } unless undefined.equals?(item)
reduce(-1) {|res, el| break res + 1 if yield el }
end
def grep(pattern)
return reduce([]) {|res, el| res.tap {|res| res << el if pattern === el }} unless block_given?
reduce([]) {|res, el| res.tap {|res| res << yield(el) if pattern === el }}
end
def group_by
reduce(Hash.new {|hsh, key| hsh[key] = [] }) {|res, el| res.tap { res[yield el] = el }}
end
def include?(obj)
reduce(false) {|res, el| break true if res || el == obj }
end
def reject
reduce([]) {|res, el| res.tap {|res| res << el unless yield el }}
end
end
Initialize multiple values from an array:
a = [1,2,3]
b, *c = a
assert_equal [b, c], [1, [2,3]]
d, = a
assert_equal d, a[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With