Ruby String to Class Name

后端 未结 7 948
感情败类
感情败类 2020-12-24 12:32

I am trying to create a new class to that will inherit from ActiveRecord::Base the class needs to be dynamically generated from a string

\"gener         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-24 13:15

    Given:

    class Banana
    end
    

    You can fetch the class in plain Ruby with:

    Object.const_get 'Banana'
    => Banana
    

    or if you're using Rails:

    'Banana'.constantize
    => Banana
    

提交回复
热议问题