Skip to content

Instantly share code, notes, and snippets.

@burlesona
Created July 21, 2016 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burlesona/ca15d7e9f5b05abd7e07be4b57c3ac7f to your computer and use it in GitHub Desktop.
Save burlesona/ca15d7e9f5b05abd7e07be4b57c3ac7f to your computer and use it in GitHub Desktop.
Ruby Method Fun
# BEFORE RUBY 2.0
def method(argument,hash)
end
method 1, :foo => "bar", :baz => "qux"
method 1, {:foo => "bar", :baz => "qux"} #identical
method(1, {:foo => "bar", :baz => "qux"}) #identical
def method(v1=1,v2=2,v3=3)
end
method # v1=1, v2=2, v3=3
method 10 #v1=10, v2=2, v3=3
method 10, 20 #v1=10, v2=20, v3=3
method 10, 20, 30 #v1=10, v2=20, v3=30
# BEFORE RUBY 2.1
def method(a=1,b:2,c:3)
end
method # a=1, b=2, c=3
method 10 # a=10, b=2, c=3
method 10, b: 20 # a=10, b=20, c=3
method 10, b: 20, c: 30 # a=10, b=20, c=30
method 10, 20, 30 # ArgumentError
# SINCE RUBY 2.1
def method(a,b:,c:)
end
method # argument error
method 10 # argument error
method 10, b: 20 # argument error
method 10, b: 20, c: 30 # a=10, b=20, c=30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment