Skip to content

Instantly share code, notes, and snippets.

@philk
Created June 7, 2018 23:22
Show Gist options
  • Save philk/5e8d058e8ecc1fdb77121984b0cd382f to your computer and use it in GitHub Desktop.
Save philk/5e8d058e8ecc1fdb77121984b0cd382f to your computer and use it in GitHub Desktop.
# 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