What does class_eval <<-“end_eval”, __FILE__, __LINE__ mean in Ruby?

前端 未结 3 1443
执笔经年
执笔经年 2020-11-30 20:21

I\'m learning how to use class_eval in modules (I\'m somewhat familiar with class_eval) and came across this helpful class in resource_controller. In there they have things

3条回答
  •  臣服心动
    2020-11-30 20:46

    Let's also note, that eval-ling strings should be avoided where possible. In your particular case, replacing #class_eval with #class_exec is possible, and should be preferred:

    class_exec do
      define_method block_accessor do |*args, &block|
        unless args.empty? && block.nil?
          args.push block if block_given?
          instance_variable_set "@#{block_accessor}", [args].flatten
        end
        instance_variable_get "@#{block_accessor}"
      end
    end
    

提交回复
热议问题