Skip to content

Instantly share code, notes, and snippets.

@rafaelsales
Created November 29, 2018 23:13
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rafaelsales/6009aa4270ab0c662b249f45108c6d05 to your computer and use it in GitHub Desktop.
Ruby bug with keyword arguments in the initializer being used via method(:new)
hash = {
1 => { c: 2 },
2 => { c: 4 },
}
class Works
def do(a, c:)
puts "a: #{a.inspect}; c: #{c.inspect}"
end
end
class DoesntWork
def initialize(a, c:)
puts "a: #{a.inspect}; c: #{c.inspect}"
end
end
puts 'Works'
hash.map(&Works.new.method(:do))
# a: 1; c: 2
# a: 2; c: 4
puts 'Does not work'
hash.map(&DoesntWork.method(:new))
# Traceback (most recent call last):
# 6: from /Users/rafaelsales/.rbenv/versions/2.5.3/bin/irb:11:in `<main>'
# 5: from (irb):36
# 4: from (irb):36:in `map'
# 3: from (irb):36:in `each'
# 2: from (irb):36:in `new'
# 1: from (irb):24:in `initialize'
# ArgumentError (missing keyword: c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment