【发布时间】:2013-12-06 02:35:38
【问题描述】:
有没有办法将符号转换为方法调用?我知道字符串可以,但符号不行。我在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
【问题讨论】:
标签: ruby