Skip to content

Instantly share code, notes, and snippets.

@jredville
Created October 5, 2012 20:22
Show Gist options
  • Save jredville/3842151 to your computer and use it in GitHub Desktop.
Save jredville/3842151 to your computer and use it in GitHub Desktop.
Allow hash or array for a loop
def hash_or_array(obj)
obj.each do |k,v=k|
puts "k: #{k}","v: #{v}"
end
end
hash_or_array([1,2,3])
# k: 1
# v: 1
# k: 2
# k: 2
# k: 3
# k: 3
hash_or_array(a:1,b:2,c:3)
# k: a
# v: 1
# k: b
# v: 2
# k: c
# v: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment