Skip to content

Instantly share code, notes, and snippets.

@carvil
Created August 2, 2012 14:23
Show Gist options
  • Save carvil/3237436 to your computer and use it in GitHub Desktop.
Save carvil/3237436 to your computer and use it in GitHub Desktop.
Initialise only once
class Bla
attr_accessor :my_time
def my_time
@my_time ||= Time.now.to_i + 100
end
end
[2] pry(main)> b = Bla.new
=> #<Bla:0x007f9b4d8660b0>
[3] pry(main)> b.my_time
=> 1343917475
[4] pry(main)> b.my_time
=> 1343917475
[5] pry(main)> b.my_time
=> 1343917475
[6] pry(main)>
@jcf
Copy link

jcf commented Jun 15, 2013

I need your Clojure XML! Can I swap it for some Ruby, please?

class Bla
  attr_reader :my_time

  def initialize
    @my_time = Time.now.to_i + 100
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment