Skip to content

Instantly share code, notes, and snippets.

@fj
Created June 13, 2010 15:50
Show Gist options
  • Save fj/436763 to your computer and use it in GitHub Desktop.
Save fj/436763 to your computer and use it in GitHub Desktop.
Helping someone who asked in #ruby about recursively replacing Array elements.
b = lambda { |e| e.kind_of?(Array) ? e.map(&b) : (e.nil? ? "" : e) }
# => #<Proc:0x0000000104d908@(irb):1 (lambda)>
arr = [5, 6, 7, nil, [nil], [6, 8, nil], [[194, 48, nil], [nil]]]
# => [5, 6, 7, nil, [nil], [6, 8, nil], [[194, 48, nil], [nil]]]
arr.map(&b)
# => [5, 6, 7, "", [""], [6, 8, ""], [[194, 48, ""], [""]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment