Created
October 5, 2012 20:22
-
-
Save jredville/3842151 to your computer and use it in GitHub Desktop.
Allow hash or array for a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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