如何获取实例的所有attr_accessor字段的哈希值?

时间:2016-12-01 11:47:38

标签: ruby oop attributes

我们有一个attr_accessor的课程。如:

class Class
  attr_accessor :x
  attr_accessor :y

  ...

end

我想从这个类的实例创建一个哈希。如:

c.to_attr_hash = { :x => "x", :y => "y" }

怎么可以这样做?

1 个答案:

答案 0 :(得分:-1)

def set_status_of_day(late_policy,early_departure_policy)
        if late_policy.warning_on_late == "Half Day" && early_departure_policy.warning_on_late == "Half Day"
            self.status_of_day = "Absent"
        elsif late_policy.warning_on_late == "Half Day" && early_departure_policy.warning_on_late == "Present"
            self.status_of_day = "Half Day"
        elsif late_policy.warning_on_late == "Half Day" && early_departure_policy.warning_on_late == "Early Departure"
            self.status_of_day = "Half Day"
        elsif late_policy.warning_on_late == "Late" && early_departure_policy.warning_on_late == "Early Departure"
            self.status_of_day = "Half Day"
        elsif late_policy.warning_on_late == "Present" && early_departure_policy.warning_on_late == "Present"
            self.status_of_day = "Present"
        .
        .
        .
        .
        .
        .
        .
        end             
end