[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

mark rudolph

4/25/2006 6:41:00 AM

I am begining to work with Modules and mixins
and I would really appreciate some insight.

For reference:
module Z
CONST=15
@inst_var=16
def inst_method1
CONST
end
def inst_method2
@inst_var
end
end

class C
include Z
def test
puts "inst_method1 = #{inst_method1}"
puts "inst_method2 = #{inst_method2}"
end
end

o=C.new
puts "#{o.test}" # -> inst_method1 = 15
# -> inst_method2 =


Although both methods appear as methods of o,
only the first returns a value - why?
What is it about @inst_var of Z that will not allow
it to be accessible in o?


Thanks very much.
2 Answers

ts

4/25/2006 8:12:00 AM

0

>>>>> "m" == mark rudolph <mrudolph@oncable.dk> writes:

m> module Z
m> CONST=15
m> @inst_var=16

this is an instance variable for the module Z (a module is an object and
can have instance variables).

m> def inst_method1
m> CONST
m> end
m> def inst_method2
m> @inst_var

this is an instance variable for a C object

m> end
m> end

m> class C
m> include Z

To see it another way

#!/usr/bin/ruby
class A
@inst_var = 'instance variable for A'

def initialize
@inst_var = 'instance variable for an instance of A'
end

def self.inst_var1
@inst_var
end

def inst_var2
@inst_var
end
end

puts A.inst_var1
puts A.new.inst_var2
moulon%

moulon% ./b.rb
instance variable for A
instance variable for an instance of A
moulon%



--

Guy Decoux

mark rudolph

4/26/2006 2:14:00 AM

0

ts wrote:
>>>>>>"m" == mark rudolph <mrudolph@oncable.dk> writes:
>
>
> m> module Z
> m> CONST=15
> m> @inst_var=16
>
> this is an instance variable for the module Z (a module is an
object and
> can have instance variables).
>
> m> def inst_method1
> m> CONST
> m> end
> m> def inst_method2
> m> @inst_var
>
> this is an instance variable for a C object
>
> m> end
> m> end
>
> m> class C
> m> include Z
>
> To see it another way
>
> #!/usr/bin/ruby
> class A
> @inst_var = 'instance variable for A'
>
> def initialize
> @inst_var = 'instance variable for an instance of A'
> end
>
> def self.inst_var1
> @inst_var
> end
>
> def inst_var2
> @inst_var
> end
> end
>
> puts A.inst_var1
> puts A.new.inst_var2
> moulon%
>
> moulon% ./b.rb
> instance variable for A
> instance variable for an instance of A
> moulon%
>


Thanks ts,

after your reply and some further testing
I think I understand.


Mark