public
Created

  • Download Gist
required.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
def required(arg=nil)
method = caller_locations(1,1)[0].label
raise ArgumentError.new("required parameter #{arg.to_sym.inspect + ' ' if arg}not passed to method #{method}")
end
 
def say(greeting: required('greeting'))
puts greeting
end
 
def say2(greeting: required)
puts greeting
end
 
def say3(greeting: required(:greeting))
puts greeting
end
 
# say
# test.rb:3:in `required': required parameter :greeting not passed to method say (ArgumentError)
# from test.rb:6:in `say'
# from test.rb:18:in `<main>'
 
# say2
# test.rb:3:in `required': required parameter not passed to method say2 (ArgumentError)
# from test.rb:10:in `say2'
# from test.rb:23:in `<main>'
 
# say3
#test.rb:3:in `required': required parameter :greeting not passed to method say3 (ArgumentError)
# from test.rb:14:in `say3'
# from test.rb:28:in `<main>'
 
# calling it like say('greeting') results in ArgumentError
 
say greeting: 'greeting'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.