Skip to content

ruby lib

irxground edited this page Feb 25, 2013 · 4 revisions

Enumerator

def alphabet
  return Enumerator.new(self, __method__) unless block_given?
  yield 'a'
  yield 'b'
  # ...
end

Hash

{a: 1, b: 2, c: 3}.fetch(:a) #=> 1
{a: 1, b: 2, c: 3}.fetch(:d) # error
{a: 1, b: 2, c: 3}.values_at(:a, :c, :x) #=> [1, 3, nil]
{a: 1, b: 2, c: 3}.except(:a, :c) #=> {b: 2}
{a: 1, b: 2, c: 3}.except(:b, :x) #=> {a: 1, c: 3}
h = {a: 1, b: 2, c: 3}
h.extract!(:a, :b) # => {a: 1, b: 2}
h                  # => {c: 3}
{a: 1, b: 2, c: 3}.slice(:a, :c) #=> {a: 1, b: 2}
Clone this wiki locally