我意识到when thinking about the user, password, and site variables in ActiveResource::Base,他们可以修改一个请求,并会保持修改下一个请求。这似乎是不受欢迎的行为,并可能是一个普遍的问题,因为它适用于所有类变量/属性的使用。 是否Rails提供了一种机制来清除对类级别状态的更改(例如重置为默认值),以便每个请求都以原始
我需要一堆类变量的,我想通过这样的一个列表循环,从而做到这一点: vars=('tx','ty','tz') #plus plenty more
class Foo():
for v in vars:
setattr(no_idea_what_should_go_here,v,0)
这可能吗?我不想让它们作为实例(在__init__中使用self),而是作为类变量。
class A
def set(v)
@@v = v
end
def put
puts @@v
end
end
class B < A
end
class C < A
end
B.new.set 'b'
B.new.put # => b
C.new.set 'c'
C.new.put # => c
B.new.pu
我有问题得到这段代码运行。这个班级是具有IdCounter的学生,这是问题出现的地方。 (在8号线) class Student:
idCounter = 0
def __init__(self):
self.gpa = 0
self.record = {}
# Each time I create a new student, the id
在Ruby的每个请求设置类变量,当我有一个像模型中的资源与宁静的资源进行通信。 资源路径有一些动态参数,所以我在每次请求之前在模型上设置一些类变量。 我有这样的事情: class MyClass << MySuperClass::Base
class << self
attr_accessor :site
attr_accessor :shop_id
at