class属性设置/获取逻辑

时间:2013-06-07 18:31:41

标签: ruby attributes logic

我如何完成以下操作?

class Monkey
    attr_setter(:name, new_name) 
        if is_supported(@version)
            @name = new_name
        else
            raise ":name is not supported for #{@version.to_s}"
        end
    end
    attr_getter(:name)
        if is_supported(@version)
            return @name
        else
            raise ":name is not supported for #{@version.to_s}"
        end
    end
end

1 个答案:

答案 0 :(得分:1)

class Monkey
    attr_reader :name 

    def name=(new_name)
        if is_supported(@version)
            @name = new_name
        else
            raise ":name is not supported for #{@version.to_s}"
        end
    end
end