gpt4 book ai didi

ruby - 方法调用的符号

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:37 25 4
gpt4 key购买 nike

<分区>

有没有办法将符号转换为方法调用?我知道它可以用于字符串,但不能用于符号。我在 conveyance.rb 中有这段代码:

def boats
boats = []
User.all.each{ |u| boats += u.boats}
boats
end
def boats_nb
self.boats.length
end
def cars
cars = []
User.all.each{ |u| cars += u.cars}
cars
end
def cars_nb
self.cars.length
end

我想知道是否有办法制作这样的方法:

def conveyances(:symb)
c = []
User.all.each{ |u| c += u.to_method(:symb) }
c
end
def conveyances_nb(:symb)
User.to_method(:symb).length
end

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com