Created
November 29, 2018 23:13
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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