Comment obtenir une classe avec un nom de classe sous forme de chaîne?
Par exemple, disons Product
classe a la méthode do_something
:
str = "product"
<what should be here based on str?>.do_something
Question 2
Comment obtenir le champ de l'objet donné un nom de champ comme une chaîne?
Par exemple, supposons que la classe Product
a price
terrain:
str = "price"
product = Product.new
product.<what should be here based on str?> = 1200
'constantize' n'est pas standard Ruby, voir http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize –
Utilisez [' public_send'] (http: // ruby- doc.org/core/Object.html#method-i-public_send) au lieu de 'send' autant que possible pour éviter de contourner la visibilité de la méthode. –