Skip to content

Instantly share code, notes, and snippets.

@zgfif
Created December 26, 2018 16:21
Show Gist options
  • Save zgfif/0ad244c084528d2c9a1df3b563716c00 to your computer and use it in GitHub Desktop.
Save zgfif/0ad244c084528d2c9a1df3b563716c00 to your computer and use it in GitHub Desktop.
def hello_message options = {}
first_name = options.fetch :first_name
last_name = options.fetch :last_name
"Hello, #{first_name} #{last_name}"
end
data = {first_name: 'Lero4ka', last_name: 'Melnik' }
# p hello_message data
def hi_message first_name:, last_name:
"Hi, #{first_name} #{last_name}"
end
# p hi_message data
def hello_message_with_an_options_hash options = {}
first_name = options.fetch :first_name
last_name = options.fetch :last_name
"Hello #{first_name} #{last_name} hello_message_with_an_options_hash"
end
def hello_message_with_keyword_arguments first_name:, last_name:
"Hello #{first_name} #{last_name} hello_message_with_keyword_arguments"
end
p hello_message_with_an_options_hash data
p hello_message_with_keyword_arguments data
p hello_message first_name: 'Pasha', last_name: 'Bratanov'
options = { last_name: 'Bratanov' }
hello_message first_name: 'Pasha', **options
def generate_thumbnail(name, width, height)
"The image '#{name}' has #{height} height and #{width} width. And #{height * width} m^2 area."
end
dimensions = [240, 320]
puts generate_thumbnail("headshot.jpg", *dimensions)
puts hello_message_with_keyword_arguments(first_name: "Justin", **options)
def hello_message(greeting, time_of_day, first_name:, last_name:)
"#{greeting} #{time_of_day}, #{first_name} #{last_name}!"
end
args = ["Morning"]
keyword_args = {last_name: "Weiss"}
p hello_message("Good", *args, first_name: "Justin", **keyword_args)
puts "Capturing the arguments the easy way:"
def argument_capturing_method *args
args
end
p argument_capturing_method 'aa','cc','xx'
p argument_capturing_method 1,2,3,key: 'Pasha'
def dual_argument_capturing_method(*args, **keyword_args)
{args: args, keyword_args: keyword_args}
end
p dual_argument_capturing_method 1,2,3, key: 'Pasha'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment