Created
August 23, 2011 23:43
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./method_test.rb | |
help1 # => nil | |
@a # => Hi, I never changed the @a variable. | |
help2 # => [:success2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.