tags:

views:

58

answers:

2

I have the following ruby code:

class Mp
  def initialize

    Test.new.mytest
    Work.new.mywork
    ha
    address

  end

  def ha
    puts "message from ha"
  end

  def address

   a='see'

  end

end


class Test

  def mytest
    m=Mp.new
    puts "Message from test do you #{m.address}"
  end
end

class Work

  def mywork
    puts "message from work"
  end

end

Mp.new

This works fine except the part in def mytest where I'm trying to put out the m.address. Thanks for your help in advance.

+2  A: 

You've got an infinite loop. You create a new object of class Mp, which in turn creates a new object of class Test and then calls its mytest method, which in turn creates another object of class Mp, which in turn...

Jeff
+2  A: 

Actually the reason it doesn't work has nothing to do with printing the address. It's one line before that:

m = Mp.new this creates a new Mp object. However inside Mp's initialize method a new Test object is created and its mytest method is called. The mytest method then again creates a new Mp object and so on. In other words: Test#mytest and Mp#initialize are mutually and infinitely recursive.

Edit in response to your comment:

I'm not quite sure I understood the question. If you mean "How do I access the variable a which was set in the address method, after address has been called": you don't. a is a local variable that goes out of scope once the method has returned. If you want to set an instance variable use @a = 'see'. @ denotes instance variables in ruby. If you want to be able to access that variable from outside the object, use attr_accessor :a to define accessor methods for @a.

An example:

class Mp
  attr_accessor :c

  def initialize
    initialize_variables
    puts @c
    puts @b # I can access @c and @b here because it's an instance variable
            # and I'm within the same object

    # puts a # This does not work because a is a local variable from the
             # initialize_variables method and no longer in scope
  end

  def initialize_variables
    a = "a"
    @b = "b"
    @c = "c"
    puts a  # I can access a here because I'm still inside the method
            # where a was defined
  end
end

m = Mp.new
# puts m.a
# puts m.b  # These don't work because there are no methods a or b

puts m.c  # This works because attr_accessor defined a method c which
          # returns the content of m's @c variable
sepp2k
I see, how then would I access the string a='see' in the def address ?
rahrahruby
So in my Mp class if I have the def address and it has a='see' I can access a in the Test by using @a? Can you post a quick example? Thanks
rahrahruby
@MAP: No. If you do `a = 'see'`, you can't access `a` at all, once the method returns. If you do `@a = 'see'`, you can access `@a`, but only within the same object - not from outside. If you do `@a = 'see'` in the method *and* `attr_accessor :a` in the class definition, you can access `@a` from outside by doing `my_mp_object.a`. I'll post an example.
sepp2k
Thanks for your help and patience!
rahrahruby
Thanks it works great!
rahrahruby