Gui Djos wrote:
> Heya fellas. I've coded C, Java and perl for a while now, but I'm
> completely new to Ruby (started off today ;) ), and I have an error
> that's bugging me. Basically, the script is intented to calculate the
> radices of an equation using the Baskara method.

And what is the error?  You neglected to say.

> 
>  Ive tried working with Float() conversions, and that hasn't given me
> the right results either. The code is as follows:
> 
> 
> [code]
> def resolveBaskara(a, b = 1, c = 1)

camelCase is usually avoided in Ruby; underscore_case is the prevailing 
style.

> 
>   if a == 0
>     puts "Not a second degree equation.\n\n"

Why the \n\n?

>     return
>   end
> 
>   delta = x_1 = x_2 = 0

This line is 100% unnecessary.  delta, x_1, and x_2 are all assigned to 
again before they're ever read.

> 
>   delta = Float((b ** 2) - (4 * a * c))

Float() is not needed here.

Also, even if it were needed, you really shouldn't be using Floats for 
math.  BigDecimal is a better choice.

> 
>   if delta < 0
>     puts "Negative Delta.\n\n"

Why the \n\n?

>     return
>   end
> 
>   x_1 = Float((-1 * b + delta ** 1/2)/2 * a)

Again, no need for Float().  And why not use sqrt(delta)?

> 
> 
>   if delta == 0
>     x_2 = x_1
>   else
>     x_2 = Float((-1 * b - delta ** 1/2)/2 * a)

Again, no need for Float().

>   end
> 
>   return x_1,x_2

You can't return multiple values; you'll have to wrap them in an array 
(your assignment statement below will still work).

> end
> 
> 
> print "Give a value for A: "
> A = gets.chomp
> print "Give a value for B: "
> B = gets.chomp
> print "Give a value for C: "
> C = gets.chomp

You probably don't want to use A, B, and C here: capitalized names are 
reserved for constants (including classes).

> 
> x1,x2 = resolveBaskara(A.to_f,B.to_f,C.to_f)
> 
>   if x1 != nil
>     puts "Raizes: \n\tX1: #{x1}"
>   end
>   if x2 != nil
>     puts "\n\tX2: #{x2}"
>   end

You don't need != nil here; since nil is a false value, you can just do 
"if x1".

> [/code]
> 
>  Again, I'm new to this, so forgive any silly mistakes / bad practices.

Are you developing test-first?  If not, that's a bad practice.

> I'll learn it when I learn it.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen / marnen.org
-- 
Posted via http://www.ruby-forum.com/.