Ruby - Using class_eval to define methods

后端 未结 3 1993
情深已故
情深已故 2020-12-23 10:45

I\'m doing the SaaS Stanford class, trying to do Part 5 of this assignment

I\'m having a really hard time grasping this concept, this is what I\'ve attempted to do:<

3条回答
  •  囚心锁ツ
    2020-12-23 11:02

    You must notice that #{attr_name}_history is a instance variable, so use @ before, like @foo in the class below

    def #{attr_name}=value, #{attr_name}= is the method name, value is parameter, same as def func parameter

    def #{attr_name}=value
      (!defined? @#{attr_name}_history) ? @#{attr_name}_history = [nil, value] : @#{attr_name}_history << value
    end
    

提交回复
热议问题