Created
December 6, 2010 16:12
-
-
Save ticktricktrack/730497 to your computer and use it in GitHub Desktop.
exercise 1 by Ray from Rubylearning Metaprogramming Batch 6
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
results = [] | |
# 3 | |
results << class A | |
def initialize | |
@a = 11 | |
@@a = 22 | |
a = 33 | |
end | |
@a = 1 | |
@@a = 2 | |
a = 3 | |
end | |
my_a = A.new | |
# 1 | |
results << A.instance_variable_get(:@a) | |
# 11 | |
results << my_a.instance_variable_get(:@a) | |
# 22 | |
results << A.class_variable_get(:@@a) | |
# 2 | |
# I had to copy the original class definition otherwise I wouldn't get 2, but 22 | |
# Maybe someone can explain | |
# class A | |
# def initialize | |
# | |
# end | |
# end | |
# | |
# puts "wrong results without repeating the original class definition" | |
# results << A.class_variable_get(:@@a) | |
class A | |
def initialize | |
@a = 11 | |
@@a = 22 | |
a = 33 | |
end | |
@a = 1 | |
@@a = 2 | |
a = 3 | |
end | |
class A | |
def initialize | |
end | |
end | |
results << A.class_variable_get(:@@a) | |
# 33 | |
class A | |
def initialize | |
@a = 11 | |
@@a = 22 | |
a = 33 | |
end | |
@a = 1 | |
@@a = 2 | |
a = 3 | |
end | |
class B < A | |
def initialize | |
puts super | |
end | |
end | |
# => last line ... | |
results.sort.each do |result| | |
puts result | |
end | |
# 33 continued | |
B.new |
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
results = [] | |
# 3 | |
results << class A | |
def initialize | |
@a = 11 | |
@@a = 22 | |
a = 33 | |
end | |
@a = 1 | |
@@a = 2 | |
a = 3 | |
end | |
# 1 | |
results << A.instance_variable_get(:@a) | |
# 2 | |
results << A.class_variable_get(:@@a) | |
# 11 | |
my_a = A.new | |
results << my_a.instance_variable_get(:@a) | |
# 22 | |
results << A.class_variable_get(:@@a) | |
# 33 | |
class A | |
def initialize | |
@a = 11 | |
@@a = 22 | |
a = 33 | |
end | |
@a = 1 | |
@@a = 2 | |
a = 3 | |
end | |
class B < A | |
def initialize | |
puts super | |
end | |
end | |
# => last line ... | |
results.sort.each do |result| | |
puts result | |
end | |
# 33 continued | |
B.new |
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
results = [] | |
# 3 | |
results << class A | |
def initialize | |
@a = 11 | |
@@a = 22 | |
a = 33 | |
end | |
@a = 1 | |
@@a = 2 | |
a = 3 | |
end | |
# 1 | |
results << A.instance_variable_get(:@a) | |
# 2 | |
results << A.class_variable_get(:@@a) | |
# 11 | |
my_a = A.new | |
results << my_a.instance_variable_get(:@a) | |
# 22 | |
# The class variable has been changed by running A.new | |
results << A.class_variable_get(:@@a) | |
# 33 | |
# Accessing a private method | |
results << my_a.send(:initialize) | |
results.sort.each do |result| | |
puts result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment