mardi 3 mars 2015

How to bypass scope convention in ruby if else statements

This seems very confusing to me.



puts "Enter Line 1 m and c:"
m1 = gets.to_f
c1 = gets.to_f

puts "Enter Line 2 m and c:"
m2 = gets.to_f
c2 = gets.to_f

if ((m1==m2) and (c1==c2))
puts "infinite solutions"
elsif ((m1==m2) and (c1!=c2))
puts "no solution"
else
x = (c1 - c2)/(m2 - m1)
y = m1*x + c1
puts "(x,y) = (" + x + "," + y+")"
end


This shows an error because ruby scope rules prevent me from accessing outer variables inside an if else block.


This is in contrast to how it works in C & C++.


Can you please tell me a way to get around this error and also explain the purpose of this style of scope conventions?


Aucun commentaire:

Enregistrer un commentaire