A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Ruby iterator cheat sheet

A list of many useful iterators in Ruby for hashes and arrays:
  • each - Do block for each item. Works on hash or array. Can use 'break'. Return nothing.
    • each_index - Same as each but pass in the index to the block
  • collect (aka map) - Do block for each item. Return array. Same as Perl map.
    • collect! / map! - Same as map, but modify array in place
  • select (aka find_all) - Do block for each item. Return array if block is true. Same as Perl grep.
    • select! (aka keep_if) - Same as select but modify array in place
    • reject - Opposite of select. Return if block is false.
    • reject! (aka delete_if) - Same as reject but modify array in place
  • inject(xyzzy) (aka reduce) - Do block for each pair of items, starting with xyzzy & first item.
  • compact - Return array with nil items removed
    • compact! - Same as collect but modify array in place
  • delete(foo) - Remove all items that are equal to foo
  • clear - Remove all items
  • empty? - Return true if array has no items
  • count - Return number of items
  • length (aka size) - Return number of items
  • flatten! - Modify array to be 1-dimensional
  • shuffle! - Shuffle in place
  • sort! - Sort in place
  • bsearch - Find item using binary search
  • combination(3) - Return all combainations of length 3
  • permutation(4) - Return all permutations of length 4
  • sort_by! - Sort in place using keys in block
  • transpose - swap rows and columns
  • zip - step through two arrays at the same time


(sourcesource, source)