From: Brian Candler Date: 2011-02-04T17:47:45+09:00 Subject: Re: problem with inheritance Lawrence Maina wrote in post #979566: > def prompt_input > > > print ("a = ") > a = gets > print ("b = ") > b = gets > print ("c = ") > c = gets > print ("d = ") > d = gets > print ("e = ") > e = gets > print ("f = ") > f = gets > print ("g = ") > g = gets > print ("h = ") > h = gets > print ("i = ") > i = gets > > > end In this code you are reading these values into local variables, which get destroyed as soon as the method returns, so the values are lost. Either: (1) Return an array of values. Put this as the last line before 'end': [a,b,c,d,e,f,g,h,i] Then the caller copies this array into its own local variable, or splats them into separate variables: a,b,c,d,e,f,g,h,i = *prompt_input (2) Read the values into instance variables: @a, @b, @c etc. They will then be available to other methods on the same object (including an instance of a subclass, which is what you were trying to do) As a hint: subclassing is almost always the wrong way to construct systems, as you can only have one parent class, and hence only inherit one behaviour. This means you have to decide which is the most important behaviour to inherit. You can use mixins (put your prompt_input method into a Module, and then include that Module into your class), but I have found that composition is normally the best approach ("has-a" instead of "is-a", that is, create an instance of a separate class, keep hold of a reference to that object, and invoke it as required) class KeyboardUserInterface attr_reader :a, :b, :c, :d, :e, :f, :g, :h, :i def prompt_input ... end end Then create an instance of KeyboardUserInterface and call its methods when you need to read values or display them. This makes it much easier to unit-test the interface separately from the matrix logic, and also you can replace the interface with a different one (say a GTk one) HTH, Brian. -- Posted via http://www.ruby-forum.com/.