Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a method only once in Ruby? Are there static variables?

Tags:

static

ruby

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?

like image 565
bastibe Avatar asked Nov 04 '10 13:11

bastibe


4 Answers

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
like image 63
Arsen7 Avatar answered Oct 19 '22 09:10

Arsen7


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    # =>
like image 30
Wayne Conrad Avatar answered Oct 19 '22 10:10

Wayne Conrad


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
like image 2
DigitalRoss Avatar answered Oct 19 '22 11:10

DigitalRoss


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
like image 2
horseyguy Avatar answered Oct 19 '22 10:10

horseyguy