Skip to content

Instantly share code, notes, and snippets.

@pratik60
Last active June 6, 2017 13:35
Show Gist options
  • Save pratik60/b281e54e7d9fc8c8b155a5448e5150a3 to your computer and use it in GitHub Desktop.
Save pratik60/b281e54e7d9fc8c8b155a5448e5150a3 to your computer and use it in GitHub Desktop.
Testing different version of arguments
### APPROACH 1 - Current System
def perform_args_async(*args)
data = args.to_json
new_args = JSON.parse(data)
perform(*new_args)
end
def perform(a, b)
puts "This is a - #{a} and b - #{b}"
end
perform_args_async(1,2)
def perform(body)
puts "This is a - #{body['a']} and b - #{body['b']}"
end
perform_args_async(a: 1, b: 2) # Legal way to do it
### APPROACH 2 - Just keyword arguments
def perform_kargs_async(**kargs)
data = kargs.to_json
new_args = JSON.parse(data).symbolize_keys
perform(**new_args)
end
def perform(a:, b:)
puts "This is a - #{a} and b - #{b}"
end
perform_kargs_async(a: 1, b: 2)
### APPROACH 3 - Hybrid arguments
def perform_modern_async(*args, **kargs)
data = { args: args, kargs: kargs }.to_json
new_data = JSON.parse(data)
new_args, new_kargs = new_data['args'], new_data['kargs']
if new_args.present? && new_kargs.present?
perform(*new_args, **new_kargs.symbolize_keys)
elsif new_args.present?
perform(*new_args)
else
perform(**new_kargs.symbolize_keys)
end
end
def perform(a, b)
puts "This is a - #{a} and b - #{b}"
end
perform_modern_async(1, 2)
def perform(a:, b:)
puts "This is a - #{a} and b - #{b}"
end
perform_modern_async(a: 1, b: 2)
def perform(fucking_crazy, a:, b:)
puts "This is #{fucking_crazy} a - #{a} and b - #{b}"
end
perform_modern_async("CRAZY!!!", a: 1, b: 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment