I wrote a script that contains a few method definitions, no classes and some public code. Some of these methods execute some pretty time-consuming shell programs. However, these shell programs only need to be executed the first time the method is invoked.
Now in C, I would declare a static variable in each method to make sure those programs are only executed once. How could I do that in Ruby?
There is an idiom in ruby: x ||= y
.
def something
@something ||= calculate_something
end
private
def calculate_something
# some long process
end
But there is a problem with this idiom if your 'long running utility' may return a false value (false or nil), since the ||=
operator will still cause the right side to be evaluated.
If you expect false values then use an additional variable, in a way similar to the proposed by DigitalRoss:
def something
return @something if @something_calculated
@something = calculate_something
@something_calculated = true
return @something
end
Don't try to save a line of code by setting the @something_calculated variable first, an then running calculate_something. If your calculate function raises an exception your function will always return nil and will never call the calculate again.
More generally, in Ruby you use instance variables. Note however, that they are visible in all the methods of given object - they are not local to the method.
If you need a variable shared by all instances, define the method in the class object, and in every instance call self.class.something
class User
def self.something
@something ||= calculate_something
end
def self.calculate_something
# ....
end
def something
self.class.something
end
end
The "memoize" gem might be good here. When you memoize a method, it is called no more than once:
require 'memoize'
include Memoize
def thing_that_should_happen_once
puts "foo"
end
memoize :thing_that_should_happen_once
thing_that_should_happen_once # => foo
thing_that_should_happen_once # =>
def f
system "echo hello" unless @justonce
@justonce = true
end
And, hmm, if you want it to run a shell command on invocation until it succeeds, you might try:
def f x
@justonce = system x unless @justonce
end
Unlike the other solutions in this thread, this solution doesn't require you to hold onto any state:
Get the method to remove itself after invocation or to overwrite itself with an empty method:
def hello
puts "hello"
define_singleton_method(:hello) {}
end
OR:
def hello
puts "hello"
singleton_class.send(:undef_method, __method__)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With