Another Basic Ruby Question

J

Jim Carter

Within the Class I define things which I would expect to be Global.
Both the matrix and the reference to class are not seen in the method
calculate, as they fall outside of the method declaration. How do I
make them visible, so they can remain where they are( Yes, I could pass
the matrix variable - e.g., calculate(m)- but is there another way to do
this?) I don't think I could pass subr in this manner.

Class Subroutines

#some subroutines listed here
def altitude(abs_psr_inhg)
#stuff
end

end


Class Ballistics

require 'Matrix'

m = Matrix[[1,2,3],[4,5,6]]
subr = Subroutines.new

def calculate

puts m
alt = subr.altitude(abs_psr_inhg)


end

end
 
J

Jesús Gabriel y Galán

Within the Class I define things which I would expect to be Global.
Both the matrix and the reference to class are not seen in the method
calculate, as they fall outside of the method declaration. =A0How do I
make them visible, so they can remain where they are( Yes, I could pass
the matrix variable - e.g., calculate(m)- but is there another way to do
this?) =A0I don't think I could pass subr in this manner.

Class Subroutines

#some subroutines listed here
=A0 def altitude(abs_psr_inhg)
=A0 =A0 =A0#stuff
=A0 end

end


Class Ballistics

=A0 require 'Matrix'

=A0 m =3D Matrix[[1,2,3],[4,5,6]]
=A0 subr =3D Subroutines.new

=A0 def calculate

=A0 =A0 =A0puts m
=A0 =A0 =A0alt =3D subr.altitude(abs_psr_inhg)


=A0 end

end

You could use instance variables. By the way, class is lowecase:

class Subroutines
def altitude(abs_psr_inhg)
puts "altitude"
end
end

class Ballistics
require 'Matrix'
def initialize
@m =3D Matrix[[1,2,3],[4,5,6]]
@subr =3D Subroutines.new
end

def calculate
puts @m
alt =3D @subr.altitude(@m)
end
end

b =3D Ballistics.new
b.calculate

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top