Skip to content

Instantly share code, notes, and snippets.

@tjaved573
Last active September 8, 2022 19:59
Show Gist options
  • Save tjaved573/fa13d1ee1fc65ace6dc217d1b0dbd623 to your computer and use it in GitHub Desktop.
Save tjaved573/fa13d1ee1fc65ace6dc217d1b0dbd623 to your computer and use it in GitHub Desktop.
splat_operator.rb
# single splat operator converts all arguments into an array.
# double splat operator converts all arguments into a hash
# order of arguments:
#// required -> optional -> variable -> keyword (use hash syntax)
def dubSplat (*c, **d)
p c
p d
end
dubSplat("ali", "mir" , bro1:["hashim"], bro2:["taimoor"], age: 23, school:"Purdue" )
#key difference
dubSplat("ali": "mir" , bro1:["hashim"], bro2:["taimoor"], age: 23, school:"Purdue" )
#! for Joining 2 hashes using double splat operator
keyword_options = {
keywords: ['Single Splat operator in Ruby', 'How to gobble up values in Ruby']
}
site_options = {
site_name: 'Ruby Splat Operator',
site_url: 'https://rubyinrails.com'
}
all_options = { **keyword_options, **site_options }
p all_options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment