Reputation: 907
I'm running this code and it show the same time but i wanted different dates so i put the sleep so it would change the time in 10 seconds.
But is not changing @date_format
r=Time.now
@date_format = r
def self.asd
puts @date_format
sleep 10
end
def self.asd1
puts @date_format
end
How can i do something like this?
Upvotes: 0
Views: 88
Reputation: 8700
If you want the time to change, you have to assign Time.now
everytime. What you have inside @date_format is the returned value, not the method.
This should work:
def self.asd
puts Time.now
sleep 10
end
def self.asd1
puts Time.now
end
Edit
Posible solution to your question on the comments:
class Test
@date_format
def self.date_format
@date_format = Time.now
end
def self.date_format=value
@date_format = value
end
end
You can use it then, like this: Test.date_format
and everytime you call that method, you'll get the new time and it'll update the variable.
Let me know if that helps.
Upvotes: 1
Reputation: 6840
Here is one approach that may work for you:
>> @date_format = lambda { Time.now }
=> #<Proc:0x007fdaa4800b98@(irb):5 (lambda)>
>> @date_format.call
=> 2011-12-21 11:20:35 -0500
>> @date_format.call
=> 2011-12-21 11:20:39 -0500
Hope it helps!
Upvotes: 2