按名称调用方法 [英] Call a method by name

查看:32
本文介绍了按名称调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何将方法作为字符串调用?例如:

Does anyone know how to call a method as a string? For example:

case @setting.truck_identification
when "Make"
  t.make
when "VIN"
  t.VIN
when "Model"
  t.model
when "Registration"
  t.registration

.to_sym 似乎不起作用.

推荐答案

use .send:

t.send @setting.truck_identification.downcase

(vin 应该是小写的以使其工作)

(vin should be downcase for it to work)

这篇关于按名称调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆