fork download
  1. # your code goes here
  2. module Foo
  3. def memo(name, &block)
  4. define_method(name) do
  5. instance_variable_set("@#{name}", block.call) unless instance_variables.include?("@#{name}".to_sym)
  6. instance_variable_get("@#{name}")
  7. end
  8. end
  9. end
  10.  
  11. Class.include(Foo)
  12.  
  13. class T
  14. memo :me do 6 + 4 end
  15. memo(:you) { 6 + 4 }
  16. memo(:them, &(->{ 6 + 4 }))
  17. end
  18.  
  19. t = T.new
  20. puts t.me
  21. puts t.you
  22. puts t.them
Success #stdin #stdout 0.01s 6200KB
stdin
Standard input is empty
stdout
10
10
10