Created
June 7, 2018 23:22
-
-
Save philk/5e8d058e8ecc1fdb77121984b0cd382f to your computer and use it in GitHub Desktop.
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
# two keyword args | |
def method_with_keyword_args(name:, greeting:) | |
puts "#{greeting} #{name}" | |
end | |
# these are the same thing, note how position doesn't matter | |
method_with_keyword_args(name: "Phil", greeting: "Hello!") | |
method_with_keyword_args(greeting: "Hello!", name: "Phil") | |
# one hash arg, opts is just a common variable name for a hash (options or optional hash depending on who you ask) | |
def method_with_hash_arg(opts) | |
puts "#{opts[:greeting]} #{opts[:name]}" | |
end | |
# these are the same thing, the curlies are optional, notice how it looks IDENTICAL to the keyword calling syntax | |
method_with_hash_arg({ name: "Phil", greeting: "Hello!" }) | |
method_with_hash_arg(name: "Phil", greeting: "Hello!") | |
# two arguments, postion matters | |
def method_with_positional_args(name, greeting) | |
puts "#{greeting} #{name}" | |
end | |
method_with_positional_args("Phil", "Hello!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment