Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created September 2, 2011 17:46
Show Gist options
  • Select an option

  • Save timuruski/1189274 to your computer and use it in GitHub Desktop.

Select an option

Save timuruski/1189274 to your computer and use it in GitHub Desktop.
Ruby and Python handle mutable arguments differently, inspired by http://blog.extracheese.org/2006/12/pythons-default-arguments-are-tricky.html
def foo(d=[]):
d.append('a')
return d
print foo() # => ['a']
print foo() # => ['a', 'a']
print foo() # => ['a', 'a', 'a']
def foo(d=[])
d << 'a'
end
p foo # => ["a"]
p foo # => ["a"]
p foo # => ["a"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment