Skip to content

Instantly share code, notes, and snippets.

@kyontan
Created June 13, 2018 06:04
Show Gist options
  • Save kyontan/1f5d6b9b93755d337138527e35c11158 to your computer and use it in GitHub Desktop.
Save kyontan/1f5d6b9b93755d337138527e35c11158 to your computer and use it in GitHub Desktop.
# [1, 2].atom_map{|x| Integer === x ? x * 2 : x }
# => [2, 4]
#
# [1, 2, [3, 4]].atom_map{|x| Integer === x ? x * 2 : x }
# => [2, 4, [6, 8]]
#
# [1, 2, [3, {k: 4, v: 3}]].atom_map{|x| Integer === x ? x * 2 : x }
# => [2, 4, [6, {:k=>8, :v=>6}]]
#
module Kernel
def atom_map(&block)
return (yield self) unless self.respond_to? :map
map{|args| args.atom_map(&block) }.yield_self{|x| Hash === self ? x.to_h : x }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment