Skip to content

Instantly share code, notes, and snippets.

@nelsnelson
Created August 23, 2011 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsnelson/1166934 to your computer and use it in GitHub Desktop.
Save nelsnelson/1166934 to your computer and use it in GitHub Desktop.
I don't get it. Can anybody explain to me what is happening here?
#! /usr/bin/env ruby
module Helper
def help1
@help1 ||= [ :success1 ]
end
def help1=(a)
@help1 = a
end
def help2
@help2 ||= [ :success2 ]
end
end
module Tester
def test
@a = "Hi, I never changed the @a variable."
if false
puts "Hi, I'm doing this."
help1 = [ :failure1 ]
@a = "This should never happen."
end
puts "help1 # => #{help1.inspect}"
puts "@a # => #{@a}"
puts "help2 # => #{help2.inspect}"
end
end
class Klass
include Helper
include Tester
def initialize
end
end
class Test
def initialize
@a = Klass.new
@a.test
end
end
Test.new if __FILE__ == $0
$ ./method_test.rb
help1 # => nil
@a # => Hi, I never changed the @a variable.
help2 # => [:success2]
@nelsnelson
Copy link
Author

And yet, if one comments out line 20 of method_test.rb, then both help1 and help2 will contain :success. I don't get it.

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