代码在 Ruby 2.7 中运行良好,但在 Ruby 2.6.3 中运行不佳
Code works well in Ruby 2.7 but not in Ruby 2.6.3
我在 Ruby 2.7 中编写了具有装饰器功能的代码。它在该版本中运行良好,但相同的代码在 Ruby 2.6 中无法正常运行。如果我删除装饰器的调用,即 wrapper_function
,那么代码将在 Ruby 2.6 中执行,但这不是我想要的功能。那么我这里的错误是什么,我该如何纠正它?
已编辑
#test.rb
FAMILYTREE = {
"name": "Royal Family",
"members": [
[
"King",
"Male",
"no mother"
],
[
"Queen",
"Female",
"no mother",
"King"
]
]
}
module Wrapper
def wrapper_function(func_name)
new_name_for_old_function = "#{func_name}_old".to_sym
alias_method(new_name_for_old_function, func_name)
define_method(func_name) do |*args, **kwargs|
begin
result = send(new_name_for_old_function, *args, **kwargs)
if result.instance_of?(Array) && result.any?
result.map(&:name).join(' ')
end
end
end
end
end
class Person
attr_accessor :name, :gender, :mother
def initialize(name, gender, mother_name=nil)
@name = name
@gender = gender
@mother = mother
end
end
class Family
extend Wrapper
attr_accessor :family_name, :members
def initialize(family_name)
@family_name = family_name
@members = []
end
def add_member(person_name, gender, mother_name, spouse_name)
person = Person.new(person_name, gender, mother_name)
members << person
end
wrapper_function(:add_member)
end
def create_family_tree(family_tree)
#Initializing the family
family_name = family_tree[:name]
members = family_tree[:members]
family = Family.new(family_name)
members.each do |name, gender, mother_name, spouse_name|
family.add_member(name, gender, mother_name, spouse_name)
end
family
end
fam = create_family_tree(FAMILYTREE)
以上是显示问题的完整文件:
给运行吧:ruby test.rb
如果版本为2.7.1
,则代码执行无误。
如果是 2.6.3,则会出现此错误:
test.rb:54:in add_member: wrong number of arguments (given 5, expected 4) (ArgumentError)
在 wrapper_function
行 23
和 25
中,如果我删除 **kwargs
则程序将执行并在版本 2.6.3
中给出所需的结果。那为什么会这样呢?
是 - 删除 wrapper_function 中的两个 **kwargs
将使其在 2.6.x 和 2.7.x
中都有效
与Ruby 2.7 deprecating automatic conversion from a hash to keyword arguments
有关
我在 Ruby 2.7 中编写了具有装饰器功能的代码。它在该版本中运行良好,但相同的代码在 Ruby 2.6 中无法正常运行。如果我删除装饰器的调用,即 wrapper_function
,那么代码将在 Ruby 2.6 中执行,但这不是我想要的功能。那么我这里的错误是什么,我该如何纠正它?
已编辑
#test.rb
FAMILYTREE = {
"name": "Royal Family",
"members": [
[
"King",
"Male",
"no mother"
],
[
"Queen",
"Female",
"no mother",
"King"
]
]
}
module Wrapper
def wrapper_function(func_name)
new_name_for_old_function = "#{func_name}_old".to_sym
alias_method(new_name_for_old_function, func_name)
define_method(func_name) do |*args, **kwargs|
begin
result = send(new_name_for_old_function, *args, **kwargs)
if result.instance_of?(Array) && result.any?
result.map(&:name).join(' ')
end
end
end
end
end
class Person
attr_accessor :name, :gender, :mother
def initialize(name, gender, mother_name=nil)
@name = name
@gender = gender
@mother = mother
end
end
class Family
extend Wrapper
attr_accessor :family_name, :members
def initialize(family_name)
@family_name = family_name
@members = []
end
def add_member(person_name, gender, mother_name, spouse_name)
person = Person.new(person_name, gender, mother_name)
members << person
end
wrapper_function(:add_member)
end
def create_family_tree(family_tree)
#Initializing the family
family_name = family_tree[:name]
members = family_tree[:members]
family = Family.new(family_name)
members.each do |name, gender, mother_name, spouse_name|
family.add_member(name, gender, mother_name, spouse_name)
end
family
end
fam = create_family_tree(FAMILYTREE)
以上是显示问题的完整文件:
给运行吧:ruby test.rb
如果版本为2.7.1
,则代码执行无误。
如果是 2.6.3,则会出现此错误:
test.rb:54:in add_member: wrong number of arguments (given 5, expected 4) (ArgumentError)
在 wrapper_function
行 23
和 25
中,如果我删除 **kwargs
则程序将执行并在版本 2.6.3
中给出所需的结果。那为什么会这样呢?
是 - 删除 wrapper_function 中的两个 **kwargs
将使其在 2.6.x 和 2.7.x
与Ruby 2.7 deprecating automatic conversion from a hash to keyword arguments
有关