1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# class C
# <statements>
# end
#
# means something like
#
# if !defined(C) then C = Class.new('C')
# <statements>_function(C, <statements>)
#
# global code -> function taking self, the self created by load/require
#
u = nil
a = 1
puts u # nil
# error puts v # undefined local
puts '- D ----------------------------'
class D
puts ' in D:'
b = 'b'
C = 1
puts ' locals: ',local_variables() # b
puts ' constants: ',constants() # C
end
puts '- global ----------------------------'
@@cls = 'cls'
puts 'locals in global code: ',local_variables() # u,a,f
# undefined class_variables(): puts 'class vars in global code: ',class_variables() # @@cls
# undefined constants(): puts 'constants in global code: ',constants() # D
puts '- class << Object.new ----------------------------'
class << Object.new
puts ' in singleton:'
class E
end
puts ' class vars:',class_variables() # @@cls
puts ' constants: ',constants() # E
class EE
end
end
puts '- F ----------------------------'
module FM
def FM.d()
puts 'static d in FM'
end
def d()
puts 'instance d in FM'
end
end
class F
puts ' in F:'
puts self #F
def m()
puts 'instance m in F:',self #<Object of F>
end
def F.m()
puts 'class m in F:',self #F
end
def F.k()
puts 'static k in F'
end
def print(v)
puts 'instance print in F'
end
include FM
def g()
puts 'in g():'
m # instance m in F
self.m # instance m in F
# k # undefined
# self.k # undefined
d # instance d in FM
self.d # instance d in FM
# F.d # undefined
FM.d # static d in FM
print 'foo' # instance print in F
end
end
f = F.new
def F.n()
puts 'class F.n:',self #F
end
def f.n()
puts 'instance f.n:',self #<Object of F>
end
f.m
F.m
f.n
F.n
f.g
# undefined method n (n is just on object f): F.new.n
puts '- class self ----------------------------'
class G
@a = 1
@@b = 2
puts 'self: ', self
puts 'self.class: ', self.class
puts 'instance: ', instance_variables()
puts 'class: ', class_variables()
puts 'self.instance: ', self.instance_variables()
puts 'self.class: ', self.class.instance_variables()
end
puts '- self in modules --------------------'
module M
puts self # M
puts self.class # Module
end
class C
a = include M # C
puts a
puts a.class
end
puts '- end ----------------------------'
|