Problem with inheritance

class Input_Agent

def intro

puts ("A three variable simultaneous equation is of the form:"),
("\n\t\t\tax+by+cz=l"), ("\t\t\tdx+ey+fz=m"), ("\t\t\tgx+hy+iz=n"),
("\nWhere the coeficients, a, b, c, d, e, f, g, h and i can take

positive or negative numbers."),
(“x, y and z are the variables whose value is to be calculated.”),
(“l, m and n can also take positive or negative numbers.”),
("\nPlease enter the values as prompted:")

end

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

end



require ‘matrix’
require ‘mathn’
require ‘input_agent’

class Matrices < Input_Agent

left_side=Input_Agent.new()
left_side.intro
left_side.prompt_input
lh=Matrix[[a,b,c],[d,e,f],[g,h,i]]

rh=Matrix[[l],[m],[n]]
inv=lh.inverse
soln=inv*rh
soln2=soln.to_a

puts (“x = #{soln2[0]}”)
puts (“y = #{soln2[1]}”)
puts (“z = #{soln2[2]}”)

end

hello there and thanks in advance. i’m new to ruby and i’m trying to
access the values i read in from the first file into the second file
through inheritance but i can’t get it to work.

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.

Thanks Brian. I declared the matrices in Solution_Agent in class
Input_Agent as methods and created their instances. Works like charm!
Thanks a great deal!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs